我是Django的新手,我遇到了一个问题。我有多种型号,需要针对地址的地址和电话号码。而且只有电话号码可以与某些型号关联。因此,我为“ 地址”和“ 电话号码”建立了多态关系。就像这里:
class PhoneNumber(SoftDeleteModel):
phone_number = PhoneNumberField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
created_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
def __str__(self):
return self.phone_number.as_e164
class Address(SoftDeleteModel):
house_number = models.CharField(max_length=255)
street_number = models.CharField(max_length=255)
area = models.CharField(max_length=255)
city = models.CharField(max_length=255)
country = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
phone_number = GenericRelation(PhoneNumber)
content_object = GenericForeignKey('content_type', 'object_id')
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
created_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
def __str__(self):
return str(self.house_number)+", "+str(self.street_number)+", "+self.area+", "+self.city+", "+self.country
这些是地址和电话号码的模型,我正在供应商中使用地址模型,例如:>
class Vendor(SoftDeleteModel):
vendor_name = models.CharField(max_length=500)
vendor_phone = GenericRelation(PhoneNumber)
vendor_address_detailed = GenericRelation(Address)
vendor_contact_person = models.CharField(max_length=500)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
created_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
def __str__(self):
return self.vendor_name
在为供应商进行管理员管理时,我只能获得地址模型中的地址,而不是 PhoneNumber ,如图中所示
管理类的代码如下:
class PhoneNumberAdmin(GenericTabularInline):
model = PhoneNumber
extra = 1
exclude = ('deleted', 'created_by')
def save_model(self, request, obj, form, change):
obj.created_by = request.user
super().save_model(request, obj, form, change)
class AddressAdmin(GenericStackedInline):
model = Address
extra = 1
exclude = ('deleted', 'created_by')
def save_model(self, request, obj, form, change):
obj.created_by = request.user
super().save_model(request, obj, form, change)
class VendorAdmin(admin.ModelAdmin):
model = Vendor
list_display = ('vendor_name', 'created_by', 'created_at_readable')
inlines = (AddressAdmin, )
exclude = ('deleted', 'created_by')
def created_at_readable(self, obj):
return naturaltime(obj.created_at)
created_at_readable.empty_value_display = "???"
请帮助我使用Django Admin添加 PhoneNumber 。
编辑:
我想将电话号码与每个地址相关联。因此,在“管理电话号码”字段中,地址将位于Adress模型中,而在“供应商”中则不会。谢谢
答案 0 :(得分:1)
在网上冲浪后,我发现了这个名为NestedAdmin的库。该库支持admin中几乎所有的嵌套关系。我要在此处发布其网址。
https://github.com/theatlantic/django-nested-admin
不过谢谢您的时间。
答案 1 :(得分:0)
在VendorAdmin中添加电话号码的内联
inlines = (AddressAdmin, PhoneNumberAdmin)