Is there any reason to set the default reverse relationship name in the current format?

149 views
Skip to first unread message

김정훈

unread,
Aug 5, 2021, 1:45:03 PM8/5/21
to Django developers (Contributions to Django itself)

Hi,

Django's default reverse relationship name is a model name followed by a _set suffix. In terms of readability, depending on the type of relationship used, I think it would be good to make the default reverse relationship name in the singular or plural form of the model name.

Is there any reason to set the default reverse relationship name in the current format?

Regards,
Jeonghun Kim

Adam Johnson

unread,
Aug 9, 2021, 6:19:21 AM8/9/21
to django-d...@googlegroups.com
Ruby on Rails uses "correct" plural forms in code. But in order to do this it contains a special library to pluralize words correctly, e.g. octopus -> octopuses. And naturally that strategy requires per-language and per-word knowledge, and can go wrong when trying to use non-english words.

Rather than use such "magic", Django takes a simple, pragmatic, predictable approach of just appending "_set". Similarly Meta.verbose_name_plural simply appends an "s". If these behaviours aren't suitable for you, you're free to override them for your project.

You can use "s" instead of "_set" for all your foreign keys with the interpolation feature of ForeignKey ( https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.ForeignKey.related_name ) plus a partial ( https://adamj.eu/tech/2021/05/05/3-uses-for-functools-partial-in-django/ ) :

from functools import partial
from django.db import models

ForeignKey = partial(models.ForeignKey, related_name="%(class)ss")


class Author(models.Model):
    ...


class Book(models.Model):
    author = ForeignKey(Author, on_delete=models.CASCADE)
    ...


--
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/2407b63e-0f51-4cf8-8308-c3a72716ace2n%40googlegroups.com.

김정훈

unread,
Aug 16, 2021, 10:52:04 PM8/16/21
to Django developers (Contributions to Django itself)
It helped a lot.

Thank you!
Jeonghun Kim
2021년 8월 9일 월요일 오후 7시 19분 21초 UTC+9에 Adam Johnson님이 작성:
Reply all
Reply to author
Forward
0 new messages