django - underscoring foreign keys - pros and cons

85 views Asked by At

As PEP8 states, one should name classes that have several words with camelcase (e.g. ProfileAttributeGroup), and use underscores for variables (profile_attribute_group).

However when it comes to django and reverse relations (and templates), we are forced to use lowercased name of classes.

For example, if our ProfileAttributeGroup has a one-to-one key to a Profile model, the reverse lookup would be profile.profileattributegroup.

Okay, we can override that one; but this lowercasing also happens in DetailView and UpdateView templates and in sql joins (e.g. someattr.filter(profileattributegroup__isnull=False)), and there's nothing we can do about it.

This makes me think that it makes sense to just lowercase foreign key names, without adding any underscores there. This way I won't have to remember when to use profile_attribute_group or profileattributegroup.

But explicit ignoring of underscores contradicts PEP8.

My question is, has anyone else had my doubts? And are there any future downsides of ignoring underscores that I haven't thought about?

1

There are 1 answers

0
Daniel Roseman On

There certainly is something you can do about it, if it bothers you that much. All relationship fields that automatically define a reverse relation also allow you to override that default by specifying related_name. So if you really don't like profile.profileattributegroup, define the one-to-one field with related_name='profile_attribute_group'.