The Django documentation: Models page mentions that the default related_name
for foreign keys uses the pattern f"{ModelClass.__name__.lower()}_set"
(or childb_set
in the specific example).
Is there a way to configure Django to convert CapWords
to cap_words
(instead of capwords
) when creating these default related_name
s?
For example, take the following models:
from django.db import models
class BlogChannel(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __str__(self):
return self.name
class ArticleAuthor(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
def __str__(self):
return self.name
class TextEntry(models.Model):
blog_channel = models.ForeignKey(Blog, on_delete=models.CASCADE)
headline = models.CharField(max_length=255)
body_text = models.TextField()
article_authors = models.ManyToManyField(Author)
number_of_comments = models.IntegerField()
number_of_pingbacks = models.IntegerField()
rating = models.IntegerField()
def __str__(self):
return self.headline
Accessing BlogChannel
records through TextEntry
is intuitive:
>>> text_entry = TextEntry.objects.first()
>>> blog_channel = text_entry.blog_channel
But the reverse is not:
>>> blog_channel.text_entry_set # should be `blog_channel.textentry_set`
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-19-0402bfca7d1f> in <module>
----> 1 blog_channel.text_entry_set
AttributeError: 'BlogChannel' object has no attribute 'text_entry_set'
Rather than modifying every foreign key field of every model to specify a related_name
that uses underscores, I'm curious if there is a setting or something to effect this style change.