显示 OneToOneField 字段类型中的所有字段
Show all field in OneToOneField field type
在 Django 1.8.3 中,我有以下 classes,它们显示在管理面板中:
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)
现在在管理面板的客户表单中,地址显示为下拉菜单。我的问题是:如何显示地址 class 的所有字段而不是客户表单中的单个下拉列表,因为它是客户 class 中的 OneToOneField?
提前致谢
如果我是你,我会更改结构,以便 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)
for field in YourModelClass._meta.get_fields():
# iterate through main model's fields
if isinstance(field, OneToOneField):
# if the field is an OneToOneField
for field2 in YourModelClass._meta.get_field(field.name).related_model._meta.get_fields():
# iterate through the OneToOneField's fields
fieldname = field2.name
fieldvalue = field2.value_from_object(getattr(instance, field.name))
# getattr simulates instance.`field.name`
else:
fieldname = field.name
fieldvalue = field.value_from_object(instance)
其中 YourModelClass 是包含更多 OneToOneField 个对象的模型 and/or其他基本型号。上例中是Address,instance是模型的实例
请注意,您不需要实例来获取字段名称,但如果您想获取字段值则需要它。
我使用此代码将模型实例转换为动态设置的上下文字典,我不确定这是最佳解决方案。
在 Django 1.8.3 中,我有以下 classes,它们显示在管理面板中:
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)
现在在管理面板的客户表单中,地址显示为下拉菜单。我的问题是:如何显示地址 class 的所有字段而不是客户表单中的单个下拉列表,因为它是客户 class 中的 OneToOneField?
提前致谢
如果我是你,我会更改结构,以便 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)
for field in YourModelClass._meta.get_fields():
# iterate through main model's fields
if isinstance(field, OneToOneField):
# if the field is an OneToOneField
for field2 in YourModelClass._meta.get_field(field.name).related_model._meta.get_fields():
# iterate through the OneToOneField's fields
fieldname = field2.name
fieldvalue = field2.value_from_object(getattr(instance, field.name))
# getattr simulates instance.`field.name`
else:
fieldname = field.name
fieldvalue = field.value_from_object(instance)
其中 YourModelClass 是包含更多 OneToOneField 个对象的模型 and/or其他基本型号。上例中是Address,instance是模型的实例
请注意,您不需要实例来获取字段名称,但如果您想获取字段值则需要它。
我使用此代码将模型实例转换为动态设置的上下文字典,我不确定这是最佳解决方案。