Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving ModelAdmin attributes (list_display, etc.) when inheriting from a custom model

I have a series of tables which I want to all have an order field. So I made an abstract model:

class OrderedModel(models.Model):
    order = models.IntegerField(default=-1)
    def _order(self):
        pass #ordering widget for changelist_view
    _order.allow_tags = True
    def save(self,*args,**kwargs):
        #set order = 0 if < 0
        super(OrderedModel,self).save(*args,**kwargs)
    class Meta:
        abstract = True

I don't want them to change the "order" field in the change_view, so I make the following ModelAdmin:

class OrderedAdmin(models.ModelAdmin):
    list_display = ("__str__","_order","order")
    list_editable = ("order",)
    readonly_fields = ("order",)

That's fine so long as every model that inherits from OrderedModel doesn't need any more items in list_display, list_editable or readonly_fields. For example, the following would generate an error because order is in list_editable but not list_display:

class Chapter(OrderedModel):
    title = models.CharField(max_length=32)

class ChapterAdmin(OrderedAdmin):
    list_display = ("title",)

I noticed that there is a get_readonly_fields that I can change to ensure that "order" gets added to readonly_fields, but there's no get_list_display or get_list_editable to over write. Is it possible to do this?

like image 440
chriscauley Avatar asked Feb 02 '26 23:02

chriscauley


1 Answers

class ChapterAdmin(OrderedAdmin):
    list_display = OrderedAdmin.list_display + ("title",)
like image 177
Simon Kagwi Avatar answered Feb 04 '26 15:02

Simon Kagwi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!