IntegrityError: null value in column “city_id ” violates not-null constraint

后端 未结 1 1427
感情败类
感情败类 2021-01-11 18:30

I two model:

class City(models.Model):
    name = models.CharField(max_length=50)
    country = models.OneToOneField(Country)

    def __unicode__(self):
            


        
相关标签:
1条回答
  • 2021-01-11 19:01
    city = models.OneToOneField(City)
    

    Are you sure you want only one user per city? I think you need

    city = models.ForeignKey(City, null=True, blank=True)
    

    null, blank because the sycndb will create a profile and will fail if no city is passed.

    Alternatively, you can pass default=default_city_id to the city ForeignKey

    0 讨论(0)
提交回复
热议问题