我有一个具有不同属性的人物模型。一个人有一个地址,它是个人/父模型的 OneToOneField。当我在添加/编辑人员时使用管理表单时,我还需要编辑地址属性,如 line1、line2、mobile 等。同样,我想从管理页面中删除一些人,它也应该删除匹配的引用,如地址。我已经尝试了很多东西,包括 Inline 等等。但是,我认为只有当地址与人有外键时,内联才能起作用,反向是不可能的。对此的任何帮助将不胜感激......
class Person(models.Model):
person_Id = models.CharField(max_length=32L, primary_key=True, db_column='PERSON_ID', editable=False)
business_Address = models.OneToOneField(Address, unique=True, db_column='BUSINESS_ADDRESS_ID')
class Meta:
db_table = 'PD_PERSON'
class Address(models.Model):
# Field names made lowercase.
address_id = models.IntegerField(primary_key=True, db_column='ADDRESS_ID')
address_name = models.CharField(max_length=256L, db_column='NAME', blank=True)
line1 = models.CharField(max_length=128L, db_column='LINE1', blank=True)
class Meta:
db_table = 'PD_ADDRESS'
在 admin.py ~
class PersonInline(admin.TabularInline):
model = Person
class PersonAdmin(admin.ModelAdmin):
list_display = ('customer_Id', 'complete_Name', 'company')
search_fields = ('name', 'customer_Id', 'email_Id')
class AddressAdmin(admin.ModelAdmin):
inlines = [ PersonInline, ]
admin.site.register(Address, AddressAdmin)
#admin.site.register(Category)
#admin.site.register(Languages)
admin.site.register(Person, PersonAdmin)