Django admin - 在同一页面上编辑父模型和相关模型

时间:2010-06-27 23:26:44

标签: python django django-admin

我希望能够在一个页面上编辑所有数据。我怎样才能实现这一目标?我应该修改我的模型吗?如果是这样,那么我应该如何修改它们呢?

class TextStyle(models.Model):
    color = models.CharField(_("color"), max_length=7)
    style = models.CharField(_("style"), max_length=30)
    typeface = models.CharField(_("typeface"), max_length=100)

class GenericText(models.Model):
    text = models.TextField(_("text"))
    lines = models.IntegerField(_("number of lines"))
    style = models.ForeignKey(TextStyle, verbose_name=_('text style'), blank=False)


class ExpirationDate(models.Model):
    date = models.DateField(_("date"))
    style = models.ForeignKey(TextStyle, verbose_name=_('text style'), blank=False)

class Coupon(models.Model):
    name = models.CharField(_("name"), max_length=100)
    slug = AutoSlugField(populate_from="title")
    background = models.ImageField(upload_to="userbackgrounds")
    layout = models.ForeignKey(Layout, verbose_name=("layout"), blank=False)
    logo = models.ImageField(upload_to="logos")
    title = models.OneToOneField(GenericText, verbose_name=("title"), blank=False, related_name="coupon_by_title")
    body = models.OneToOneField(GenericText, verbose_name=("body"), blank=False, related_name="coupon_by_body")
    disclaimer = models.OneToOneField(GenericText, verbose_name=("disclaimer"), blank=False, related_name="coupon_by_disclaimer")
    promo_code = models.OneToOneField(GenericText, verbose_name=("promo code"), blank=False, related_name="coupon_by_promo")
    bar_code = models.OneToOneField(BarCode, verbose_name=("barcode"), blank=False, related_name="coupon_by_barcode")
    expiration = models.OneToOneField(ExpirationDate, verbose_name=("expiration date"), blank=False, related_name="coupon_by_expiration")
    is_template = models.BooleanField( verbose_name=("is a template"), )
    category = models.ForeignKey(Category, verbose_name=("category"), blank=True,null=True, related_name="coupons")
    user = models.ForeignKey(User, verbose_name=("user"), blank=False)

2 个答案:

答案 0 :(得分:1)

您需要在admin.py中创建内联模型。见:InlineModelAdmin

答案 1 :(得分:0)

我创建了一个用于内联编辑OneToOne关系的模块,我称之为ReverseModelAdmin。你可以找到它here

您可以在优惠券实体上使用它来获取内联的所有OneToOne关系:

class CouponAdmin(ReverseModelAdmin):
    inline_type = 'tabular'
admin.site.register(Coupon, CouponAdmin)
警告说。我不得不攻击许多内部组件以使其工作,因此解决方案很脆弱并且可以轻松破解。