I build software with Python, Django, ReactJS & React-Native. Every day. All day long.

Django And related_names

Be A Good Citizen And Create Good Related Names

If you are a Django developer and if you create reusable apps, please do everyone else a favour: be a good citizen and add related names to all your foreign keys.

Why? For example, you cannot add Mezzanine and django-cms to the same project because both apps have a Page model with a foreign key to Django's Site model and both apps didn't provide a related name. Suckers!

When you finally add a related name, try to come up with name that is unlikely to cause clashes with any other app out there. The safest way to do this is to simply prepend your appname to the related name.

Bad example:

1
2
3
4
5
class Page(models.Model):
    site = models.ForeignKey(
        Site,
        related_name='pages',
    )

This doesn't help at all because it is very likely that some other app also has a Page model and would also set the same related name, which would lead to a clash again.

Good example:

1
2
3
4
5
class Page(models.Model):
    site = models.ForeignKey(
        Site,
        related_name='myapp_pages',
    )

This is good because it's quite unlikely that there will be another app which

  1. has the same name as your app
  2. and has a Page model
  3. and uses this very same foreign key and related name.

Even if there is an app with the same name, you would probably not be able to use both apps at the same time because they would have the same package name.

 
comments powered by Disqus