在OneToOneField字段类型中显示所有字段

时间:2015-07-13 06:07:46

标签: django django-models django-forms django-admin

在Django 1.8.3中,我有以下类,它们在管理面板中显示:

class Address(models.Model):
    address_line1 = models.CharField(max_length=64)
    address_line2 = models.CharField(max_length=64)
    address_line3 = models.CharField(max_length=64)
    post_code = models.CharField(max_length=5)


class Customer(models.Model):
    name = models.CharField(max_length=64)
    address = models.OneToOneField(Address)

现在,在管理面板中的客户表单中,地址显示为下拉菜单。我的问题是:我如何显示Address类的所有字段而不是Customer表单中的单个下拉列表,因为它是Customer类中的OneToOneField?

提前致谢

1 个答案:

答案 0 :(得分:1)

如果我是你,我会更改结构,以便Address可以是内联的。在models.py中:

class Customer(models.Model):
    name = models.CharField(max_length=64)

class Address(models.Model):
    costumer = models.OneToOneField(Costumer)
    address_line1 = models.CharField(max_length=64)
    address_line2 = models.CharField(max_length=64)
    address_line3 = models.CharField(max_length=64)
    post_code = models.CharField(max_length=5)

然后,在admin.py中:

class AddressInline(admin.StackedInline):
    model = Address
    extra = 1
    max_num = 1

class CostumerAdmin(admin.ModelAdmin):
    inlines = [AddressInline]


admin.site.register(Costumer, CostumerAdmin)