比方说,我们正在制作一个网站,客户(Customer)可以来这里给出版社(PublishingHouse)评分。我们需要存储Customer和PublishingHouse的地址,所以我们创建一个Address类,如下所示:
class Address(models.Model):
line1 = models.CharField(max_length=50)
line2 = models.CharField(max_length=50)
city = models.CharField(max_length=50)
postal_code = models.CharField(max_length=6)
country = models.CharField(max_length=50)然后我们的Customer和PublishingHouse类有对它的引用,如下所示:
class Customer(models.Model):
name = models.CharField(max_length=50)
address = models.ForeignKey(Address)
class PublishingHouse(models.Model):
name = models.CharField(max_length=50)
website = models.URLField(max_length=4096)
address = models.ForeignKey(Address)这对我在django shell中的交互会话很有效,但是当我为这些模型激活管理时,有没有一种方法可以让Address中的字段出现在添加客户或添加出版社页面上?
我正在使用django 1.5
发布于 2013-03-14 19:25:20
你可能在找generic inlines。根据文档中的example,Address将是您的Image模型
根据您的评论,您可以使用类继承:
class Address(models.Model):
# fields
class DynamicAddress(Address):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey("content_type", "object_id")
# etc您还可以使用泛型内联管理类上的max_num属性限制(泛型)内联。
https://stackoverflow.com/questions/15407857
复制相似问题