Improved labels for M2M relations

109 views
Skip to first unread message

Julian Klotz

unread,
Oct 26, 2020, 12:31:10 PM10/26/20
to Django developers (Contributions to Django itself)

Hey there,

here’s a little topic that impacts the usability of Django admin’s delete views.

When deleting an object using django admin, you get a nice summary of all deleted objects.
I really like the overview, but the labels used to describe m2m relations that will be deleted aren't user-friendly at all:

[…]
  • MediaItem-subseries-relation: MediaItem_sub_series_relation object (11482)
  • MediaItem-piece-relation: MediaItem_pieces_relation object (11773)
So I’d like to improve the labels a bit, using the from and to model’s verbose names. This can be easily accomplished by changing `create_many_to_many_intermediary_model` in fields/related.py.

To be honest, I don’t know how to update the tests for something rooted deeply in Django’s core.Is anyone who is more competent than me interested in fixing this? It would greatly improve the “delete confirmation“ views.

Here’s what I’ve got so for; it only fixes the model’s verbose name, not its string presentation.

Best regards
Julian


[…]
    to = make_model_tuple(to_model)[1]
    from_ = klass._meta.model_name

    if isinstance(to_model, str):
        to_label = to
        to_label_plural = to
    else:
        to_label = to_model._meta.verbose_name
        to_label_plural = to_model._meta.verbose_name_plural

    from_label = klass._meta.verbose_name
    from_label_plural = klass._meta.verbose_name_plural

    if to == from_:
        to = 'to_%s' % to
        from_ = 'from_%s' % from_

    meta = type('Meta', (), {
        'db_table': field._get_m2m_db_table(klass._meta),
        'auto_created': klass,
        'app_label': klass._meta.app_label,
        'db_tablespace': klass._meta.db_tablespace,
        'unique_together': (from_, to),
        'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_label, 'to': to_label},
        'verbose_name_plural': _('%(from)s-%(to)s relationships') % {'from': from_label_plural, 'to': to_label_plural},
        'apps': field.model._meta.apps,
    })


Adam Johnson

unread,
Oct 26, 2020, 2:56:15 PM10/26/20
to django-d...@googlegroups.com
Hi Julian

It should be noted that you can improve the display today by creating explicit through models. However this is more longwinded.

This change does look like a neat usability improvement to me. It can also help with debugging as the label appears in the default str/repr. My main question is, does this not trigger migrations on user projects?

Thanks,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/69b6f2e0-8f0e-4cd0-b608-46fb9040b355n%40googlegroups.com.


--
Adam

Julian Klotz

unread,
Oct 27, 2020, 4:30:59 AM10/27/20
to Django developers (Contributions to Django itself)
Hi Adam,

thanks for picking this up so quickly.
I considered setting custom “through” models to set verbose name and __str__, but I currently can’t “afford” changing all m2m fields throught the application.

Here’s the feedback I got concerning the usability issues (e.g. for MediaItem_pieces_relation object (11773))
  • Some people thought “11773” was the number of objects to be deleted, even though the UI clearly states something else – it isn’t obvious for some people that relations have their own IDs
  • People asked where they could change the (then-deleted) relation to a different object


It can also help with debugging as the label appears in the default str/repr.
Thanks for giving me that hint. It’s the __str__ method of models.Model. Changing the default string presentation may be dangerous and also too generic. It would make sense to define a custom to-string method in `create_many_to_many_intermediary_model` (fields/related.py).
Instead of showing MediaItem_pieces_relation object (11773), something like “Media Item-Piece relation, ID: 11773) would be good; moreover, it may be a good idea to show the “from” model’s edit link to point people to the place where they can change this relation.

My main question is, does this not trigger migrations on user projects?
Good point. I just checked – interestingly, neither changing the verbose name nor the __str__ method triggers new migrations 🥳



Best regards
Julian

Julian Klotz

unread,
Oct 27, 2020, 4:56:19 AM10/27/20
to Django developers (Contributions to Django itself)
… one last thought:

The verbose name could be: “Media Item-Piece relation”
The string presentation could be: “Relation to <string presentation of to-instance>”, e.g. “Relation to Piece xyz”

Together: Media Item-Piece relation: Relation to Piece xyz (ID: 11773)

Adam Johnson

unread,
Nov 2, 2020, 4:21:06 AM11/2/20
to django-d...@googlegroups.com
I don't think we can use just "Relation to" because M2M relationships are two-way by default. Whilst a ManyToManyField can be declared on one model, it adds a reverse field on the other model. We'd have to show both sides of the relation e.g. "Relation between <x> and <y>".



--
Adam

Julian Klotz

unread,
Nov 3, 2020, 5:36:23 AM11/3/20
to Django developers (Contributions to Django itself)
Hey there, thanks! I agree, "Relation between <x> and <y>" will work in all cases.

Here’s a draft implementation:

Here’s what needs to be improved:
  • `create_many_to_many_intermediary_model`: The method uses both model classes and model class names; to access the verbose name, class names need to be resolved to “real” classes first. Any hints how to do that? `resolve_relation` doesn't to the job, because it returns both model names and classes, too.
  • Not sure how to implement tests for __str__ method without creating migrations?
Let me know what you think about it.

Julian
Reply all
Reply to author
Forward
0 new messages