Django - 在管理面板中显示具有内容类型的模型
Django - Displaying model having content type in admin panel
我是 Django 的新手,我遇到了一个问题。我有多个模型需要地址和 phone 数字地址。并且部分机型只能绑定phone个号码。因此,我为“Addresses”和“PhoneNumber”建立了多态关系。喜欢这里:
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
这些是 Address 和 PhoneNumber 的模型 我在 Address 中使用 [= =28=]供应商喜欢:
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
当我在管理员中为供应商制作 crud 时,我只能在 地址中获得 地址 而不是 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。
编辑:
我想将 phone 个数字与每个地址相关联。因此,在管理员中,PhoneNumber 字段将位于 Adress 模型中,而不是 Vendor 中。谢谢
在供应商管理中添加内联电话号码
inlines = (AddressAdmin, PhoneNumberAdmin)
在网上冲浪后,我发现了这个名为 NestedAdmin 的库。这个库支持管理中几乎所有类型的嵌套关系。我要 post 它的 url 这里。
https://github.com/theatlantic/django-nested-admin
谢谢你的时间。
我是 Django 的新手,我遇到了一个问题。我有多个模型需要地址和 phone 数字地址。并且部分机型只能绑定phone个号码。因此,我为“Addresses”和“PhoneNumber”建立了多态关系。喜欢这里:
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
这些是 Address 和 PhoneNumber 的模型 我在 Address 中使用 [= =28=]供应商喜欢:
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
当我在管理员中为供应商制作 crud 时,我只能在 地址中获得 地址 而不是 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。
编辑:
我想将 phone 个数字与每个地址相关联。因此,在管理员中,PhoneNumber 字段将位于 Adress 模型中,而不是 Vendor 中。谢谢
在供应商管理中添加内联电话号码
inlines = (AddressAdmin, PhoneNumberAdmin)
在网上冲浪后,我发现了这个名为 NestedAdmin 的库。这个库支持管理中几乎所有类型的嵌套关系。我要 post 它的 url 这里。
https://github.com/theatlantic/django-nested-admin
谢谢你的时间。