Opened 11 years ago
Closed 11 years ago
#22778 closed New feature (fixed)
Specify default value for a Model's RelatedFields related_name.
Reported by: | Owned by: | anonymous | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | jorgecarleitao@…, renaud.parent@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
It would be nice to be able to define a specific default "model-based" related_name value for RelatedField fields. Currently it is <model_name>_set, and can be overriden when defining one field.
E.g.
class Author(models.Model): # ... pass class Editor(models.Model): # ... pass class Book(models.Model): editor = models.ForeignKey(Editor, related_name="books") authors = models.ManyToManyField(Author, related_name="books")
It would be nice to be able to define the model Book in some way that would look like this :
class Book(models.Model): class Meta: default_related_name = "books" editor = models.ForeignKey(Editor) authors = models.ManyToManyField(Author)
See discussion at http://stackoverflow.com/questions/24062280/django-is-it-possible-to-define-a-class-related-name-for-all-relational-fields
Change History (18)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Version: | 1.6 → master |
comment:3 by , 11 years ago
comment:4 by , 11 years ago
Needs documentation: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
This seems to be good idea. I'm surprised it didn't come up before. I searched Trac's achive of tickets without success.
comment:6 by , 11 years ago
I have submitted a pull request at https://github.com/django/django/pull/2785
All tests pass under sqlite
comment:7 by , 11 years ago
Needs documentation: | unset |
---|---|
Patch needs improvement: | set |
Comments for improvement on PR. Please uncheck "Patch needs improvement" when you update it, thanks.
comment:8 by , 11 years ago
Cc: | added |
---|---|
Needs tests: | set |
Hi RenaudParent, thanks for the patch.
I've reviewed your PR and made minor comments on the PR in github. It seems promising.
As far as I can tell, the PR still requires:
- to address Meta subclassing: i.e. what happens when the Model is subclassed? At the moment it seems the
default_related_name
is inherited, which can cause a clash; see [1].
- tests: it passes all tests means it is compatible with current Django functionality, which is great; however, we must ensure it is also compatible with what we want to achieve with it, namely, that if we add a
default_related_name
toMeta
, the relations withoutrelated_name
get the default one, while others don't. We may also want to test subclassing.
- documentation: besides being in the API for Meta, it would be nice to document it in other places, specially in
docs/db/models
, section "Be careful withrelated_name
".
(after 1.-3.)
- There should be an item in
docs/releases/1.X.rst
(X=8?) describing this new functionality.
- If core devs agree, refactor tests where this new functionality can be useful. For example, the model
Book
intests/costum_managers/models.py
seems a good candidate.
[1] https://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name
comment:9 by , 11 years ago
I have pushed a new commit based on previous comments here : https://github.com/RenaudParent/django/commit/8710030d4c52b1c542e735fdf026fdcd0842518a
comment:10 by , 11 years ago
Patch needs improvement: | unset |
---|
comment:14 by , 11 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | set |
comment:15 by , 11 years ago
I have pushed a new commit based on comments.
https://github.com/RenaudParent/django/commit/3c1ed1ff97a306dc5b907551c09cdefe3258b3b3
comment:16 by , 11 years ago
Patch needs improvement: | unset |
---|
comment:17 by , 11 years ago
Cc: | added |
---|
comment:18 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Would something like this be ok ?
https://github.com/RenaudParent/django/commit/ea49025a4eefeb01fed27b118630b8991318a64f