Django unique, null and blank CharField giving 'already exists' error on Admin page

前端 未结 7 2041
旧巷少年郎
旧巷少年郎 2021-01-01 13:30

I\'ve been getting the most weird error ever. I have a Person model

class Person(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    f         


        
7条回答
  •  礼貌的吻别
    2021-01-01 14:05

    In Django 1.11, form CharFields will have an empty_value argument, which allows you to use None if the field is empty.

    Model forms, including the Django admin, will automatically set empty_value=None if the model's CharField has null=True.

    Therefore, you will be able to use null=True, blank=True and unique=True together in your model CharField without the unique constraint causing problems.

提交回复
热议问题