开发者

Django admin - Edit parent model and related models on the same page

开发者 https://www.devze.com 2023-01-05 02:25 出处:网络
I want to be able to edit all data on one page. How can i achieve this ? Should i modify my models? If so, then how should i modify them?

I want to be able to edit all data on one page. How can i achieve this ? Should i modify my models? If so, then how should i modify them?

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, rel开发者_运维百科ated_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)


You need to create an inline model in your admin.py. See: InlineModelAdmin.


I have created a module for inline editting of OneToOne relationships which i called ReverseModelAdmin. You can find it here.

You could use it on your Coupon entity to get all OneToOne relationships inlined like this:

class CouponAdmin(ReverseModelAdmin):
    inline_type = 'tabular'
admin.site.register(Coupon, CouponAdmin)

Caveat emptor. I had to hack into lots of internals to make it work, so the solution is brittle and can break easily.

0

精彩评论

暂无评论...
验证码 换一张
取 消