我正在尝试使用 django 管理界面以单一形式插入场地和地址(映射 1:1)。
在我的模型中:
class Venue(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Address(models.Model):
states = {
...
}
countries = {
...
}
venue = models.OneToOneField('clubApp.Venue')
address_line1 = models.CharField("Address line 1", max_length=50)
address_line2 = models.CharField("Address line 2", max_length=50, blank=True)
suburb = models.CharField("Suburb", max_length=40)
state = models.CharField(max_length=3, choices=states)
country = models.CharField(max_length=9, choices=countries)
def __unicode__(self):
return "%s %s, %s, %s, %s" % (self.address_line1, self.address_line2, self.suburb, self.state, self.country)
在我的 admin.py 中:
class AddressInline(admin.StackedInline):
model = Address
class VenueAdmin(admin.ModelAdmin):
inlines = [
AddressInline
]
admin.site.register(Venue, VenueAdmin)
当我插入时,我得到一个完整性错误,“address_id”列中的空值违反了非空约束,我认为这是由于在地址之前将场地插入数据库(postgres)。解决此问题的正确方法是什么?