Opened 15 hours ago
Closed 12 hours ago
#36041 closed Bug (invalid)
related_query_name return wrong related_name
Reported by: | mhmk2002 | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
By default djanog set a related name with this pattern model_name + "_set".
but in related_query_name in django/db/models/fields/related.py this is wrong.
Now is this
`
(
self.remote_field.related_query_name
or self.remote_field.related_name
or self.opts.model_name
)
`
`
and most change to this
(
self.remote_field.related_query_name
or self.remote_field.related_name
or f'{self.opts.model_name}_set'
)
`
suppost we have some model like this:
`
class Collection(models.Model):
title = models.CharField(max_length=255)
featured_product = models.ForeignKey('Product', on_delete=models.SET_NULL, null=True,)
class Product(models.Model):
title = models.CharField(max_length=255)
description = models.TextField
price = models.DecimalField(max_digits=6, decimal_places=2)
inventory = models.IntegerField
last_update = models.DateTimeField(auto_now=True)
collection = models.ForeignKey(Collection, on_delete=models.PROTECT)
`
this related_name is okay but django will get error beacuse this function is wrong.
related_query_name
andrelated_name
are two distinct concepts. If you're having trouble understanding how Django works, see TicketClosingReasons/UseSupportChannels for ways to get help.