Opened 2 years ago

Closed 2 years ago

#34090 closed Cleanup/optimization (invalid)

ordering of ManyToManyField in admin shows objects multiple times

Reported by: spechtx Owned by: nobody
Component: contrib.admin Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by spechtx)

I might came across a possible optimization in the admin:

When there is a model with a ManyToManyField and you set this field in the "ordering" setup in the admin setup, the object will we shown as many times in the admin, as there are relations stored in the m2m field.

In this example, when the "type" field has 5 relations, the object will be shown 5 times in the admin.

models.py:

class RelatedM2M(models.Model):
choice = models.CharField(max_length=255, unique=True)
description = models.TextField(blank=True)

class ModelName(models.Model):
id_uuid = models.UUIDField(...)
timestamp_created = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=255)
type = models.ManyToManyField(RelatedM2M, related_name='type')
type_intermediate = models.ManyToManyField(AnotherRelatedM2M, through='RelatedIntermediate')
geometry = models.GeometryField(...)

admin.py:

@admin.register(ModelName)
classModelNameAdminView(admin.GISModelAdmin):
inlines = (RelatedIntermediateInline,)
autocomplete_fields = ['...']
ordering = ['name', 'type', 'timestamp_created', ]

Change History (4)

comment:1 by spechtx, 2 years ago

Description: modified (diff)

comment:2 by spechtx, 2 years ago

Description: modified (diff)

comment:3 by spechtx, 2 years ago

Description: modified (diff)

comment:4 by Mariusz Felisiak, 2 years ago

Resolution: invalid
Status: newclosed

This is a documented behavior, see note in the QuerySet.order_by() docs.

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