Unique fields that allow nulls in Django

前端 未结 10 1057
闹比i
闹比i 2020-11-28 01:56

I have model Foo which has field bar. The bar field should be unique, but allow nulls in it, meaning I want to allow more than one record if bar field is null,

10条回答
  •  醉话见心
    2020-11-28 02:35

    Another possible solution

    class Foo(models.Model):
        value = models.CharField(max_length=255, unique=True)
    
    class Bar(models.Model):
        foo = models.OneToOneField(Foo, null=True)
    

提交回复
热议问题