Simplified example of what I wrote today:
class Grandparent(models.Model):
text = models.CharField(max_length=5, choices=too_many)
class Parent(models.Model):
grandparent = models.ForeignKey(Grandparent, on_delete=models.CASCADE)
info = models.CharField(max_length=30)
class ChildSomething(models.Model):
parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
data = models.CharField(max_length=5, choices=chromosome_identities)
Given a specific Grandparent object, I wanted to order Parent objects underneath it by most ChildSomething's each Parent has, which I did successfully below.
grandparent = Grandparent.objects.get(id=32923) # arbitrary id
# This query produces the desired result
parents = (
Parent.objects.filter(grandparent=grandparent).
annotate(num_child_somethings = Count('childsomething')).
order_by('num_child_somethings')
)
My question: Why is it Count('childsomething') instead of Count('child_something')? The latter was my intuition, which was corrected by a helpful Django error message:
Exception Type: FieldError
Exception Value:
Cannot resolve keyword 'user_translation' into field. Choices are: id, translate_text, translate_text_id, translation, usertranslation
The answer to this is likely simple, I just couldn't find anything in the docs related to this. How does the naming convention work? If anyone knows of the appropriate Django docs link that would be ideal. Thank you.