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
--
Ticket URL: <https://code.djangoproject.com/ticket/22778>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:1>
* owner: nobody => anonymous
* status: new => assigned
* version: 1.6 => master
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:2>
Comment (by anonymous):
Would something like this be ok ?
https://github.com/RenaudParent/django/commit/ea49025a4eefeb01fed27b118630b8991318a64f
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:3>
* needs_docs: 0 => 1
* stage: Unreviewed => Accepted
Comment:
This seems to be good idea. I'm surprised it didn't come up before. I
searched Trac's achive of tickets without success.
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:4>
Comment (by RenaudParent):
I have started a working branch for this ticket, it is available here :
https://github.com/RenaudParent/django/tree/ticket_22778
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:5>
Comment (by RenaudParent):
I have submitted a pull request at
https://github.com/django/django/pull/2785
All tests pass under sqlite
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:6>
* needs_docs: 1 => 0
* needs_better_patch: 0 => 1
Comment:
Comments for improvement on PR. Please uncheck "Patch needs improvement"
when you update it, thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:7>
* cc: jorgecarleitao@… (added)
* needs_tests: 0 => 1
Comment:
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:
1. The patch doesn't addressed Meta subclassing yet: 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].
2. 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` to `Meta`, the relations without `related_name` get
the default one, while others don't. We may also want to test subclassing.
3. 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 with `related_name`".
(after 1.-3.)
4. There should be an item in `docs/releases/1.X.rst` (X=8?) describing
this new functionality.
5. If core devs agree, refactor tests where this new functionality can be
useful. For example, the model `Book` in `tests/costum_managers/models.py`
seems a good candidate.
[1] https://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-
with-related-name
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:8>
Comment (by RenaudParent):
I have pushed a new commit based on previous comments here :
https://github.com/RenaudParent/django/commit/8710030d4c52b1c542e735fdf026fdcd0842518a
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:9>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:10>
Comment (by Tim Graham <timograham@…>):
In [changeset:"5a3ae7e260f68602b25dbe3fc6ed13249a5c6515"]:
{{{
#!CommitTicketReference repository=""
revision="5a3ae7e260f68602b25dbe3fc6ed13249a5c6515"
Created a new tests folder (`model_options`).
And moved `tablespaces` option tests to it.
The new folder can be used to test models/options, like the new option
added in refs #22778.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:11>
Comment (by Tim Graham <timograham@…>):
In [changeset:"7bd2ad1dd9fc449ed1b4832fab5c975d7c8aa895"]:
{{{
#!CommitTicketReference repository=""
revision="7bd2ad1dd9fc449ed1b4832fab5c975d7c8aa895"
[1.7.x] Created a new tests folder (`model_options`).
And moved `tablespaces` option tests to it.
The new folder can be used to test models/options, like the new option
added in refs #22778.
Backport of 5a3ae7e260 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:12>
Comment (by RenaudParent):
Ok thanks, I just updated my commit.
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:13>
* needs_better_patch: 0 => 1
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:14>
Comment (by RenaudParent):
I have pushed a new commit based on comments.
https://github.com/RenaudParent/django/commit/3c1ed1ff97a306dc5b907551c09cdefe3258b3b3
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:15>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:16>
* cc: renaud.parent@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:17>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"87d0a3384cc263fe0df749a28e2fbc1e1240f043"]:
{{{
#!CommitTicketReference repository=""
revision="87d0a3384cc263fe0df749a28e2fbc1e1240f043"
Fixed #22778 -- Added a model Meta option to define default_related_name.
Thanks jorgecarleitao and mmardini for reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:18>