Opened 6 years ago

Closed 6 years ago

#29354 closed Bug (invalid)

ordering inherited from abstract superclass does not get applied to admin inlines

Reported by: Moritz Pfeiffer Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords: ordering inheritance abstract meta
Cc: Mariusz Felisiak Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The attached project illustrates a problem where Meta.ordering inherited from an abstract superclass does not get applied to admin inlines.

Suppose we have the following models:

from django.db import models


class Orderable(models.Model):
    sort_order = models.IntegerField(null=True, blank=True)

    class Meta:
        abstract = True
        ordering = ['sort_order']


class Foo(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'foo'
        verbose_name_plural = 'foos'


class Bar(Orderable, models.Model):
    foo = models.ForeignKey(Foo, blank=True, null=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

    def __str__(self):
        return '{}-{}'.format(self.name, self.sort_order)

    class Meta:
        verbose_name = 'bar'
        verbose_name_plural = 'bars'
        # ordering = ['sort_order']  # needs to be explicitly set to effect admin inline ordering

and the following admin definition:

from django.contrib import admin
from metainheritance.models import Foo, Bar


class BarInline(admin.StackedInline):
    model = Bar
    fields = ['foo', 'name', 'sort_order']
    extra = 0


class FooAdmin(admin.ModelAdmin):
    inlines = [BarInline]

admin.site.register(Foo, FooAdmin)


Then BarInline objects in the admin interface will only be ordered by sort_order if it is explicitly defined on Bar.Meta.

In the attached project, there is a custome management command python manage.py create_data to initialize the database.

Attachments (1)

meta_inheritance_test.zip (11.6 KB ) - added by Moritz Pfeiffer 6 years ago.
Test project that illustrates the problem

Download all attachments as: .zip

Change History (3)

by Moritz Pfeiffer, 6 years ago

Attachment: meta_inheritance_test.zip added

Test project that illustrates the problem

comment:1 by Mariusz Felisiak, 6 years ago

Cc: Mariusz Felisiak added

IMO Bar.Meta should inherit from Orderable.Meta (please see https://docs.djangoproject.com/en/stable/topics/db/models/#meta-inheritance).

Last edited 6 years ago by Tim Graham (previous) (diff)

comment:2 by Tim Graham, 6 years ago

Resolution: invalid
Status: newclosed

That analysis looks correct to me.

Note: See TracTickets for help on using tickets.
Back to Top