Is it possible to create snake case `related_name` from class interpolation in Django related fields?

Viewed 352

Django allows us to use '%(class)s' to automatically create related name from mixins. But when I have a ClassName I'd rather access it using class_name, not classname. I know it's only semantics but I was wondering if it's possible to adjust model field to make it snake case instead.

I was browsing through django.db.models.fields but I can't find where the class interpolation is happening.

Example code

from django.db import models


class User(models.Model):
    pass


class UserRelatedMixin(models.Model):
    user = models.OneToOneField(
        to=User,
        parent_link=True,
        on_delete=models.CASCADE,
        related_name='%(class)s',
        related_query_name="%(class)s",
    )

    class Meta:
        abstract = True


class HomeAddress(UserRelatedMixin):
    pass


user = User.objects.first()

What I have

user.homeaddress

What I want instead

user.home_address

Right now I'm using a @property but it won't allow ORM queries so it's a partial solution.

0 Answers
Related