[Django] #22778: Specify default value for a Model's RelatedFields related_name.

41 views
Skip to first unread message

Django

unread,
Jun 6, 2014, 8:45:02 AM6/6/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
----------------------------------------------+--------------------
Reporter: renaud.parent@… | Owner: nobody
Type: New feature | Status: new
Component: Database layer (models, ORM) | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
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

--
Ticket URL: <https://code.djangoproject.com/ticket/22778>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 6, 2014, 8:53:02 AM6/6/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------

Reporter: renaud.parent@… | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.6
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by RenaudParent):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:1>

Django

unread,
Jun 6, 2014, 8:57:46 AM6/6/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master

(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by RenaudParent):

* owner: nobody => anonymous
* status: new => assigned
* version: 1.6 => master


--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:2>

Django

unread,
Jun 6, 2014, 9:03:40 AM6/6/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 6, 2014, 11:36:27 AM6/6/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* 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>

Django

unread,
Jun 6, 2014, 12:22:21 PM6/6/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 10, 2014, 5:39:14 AM6/10/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 1
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 10, 2014, 12:00:26 PM6/10/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 1

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* 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>

Django

unread,
Jun 10, 2014, 12:01:28 PM6/10/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by jorgecarleitao):

* 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>

Django

unread,
Jun 11, 2014, 9:26:24 AM6/11/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 1
Needs tests: 1 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 11, 2014, 9:26:44 AM6/11/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0

Needs tests: 1 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by RenaudParent):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:10>

Django

unread,
Jun 11, 2014, 11:40:04 AM6/11/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 1 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 11, 2014, 11:40:37 AM6/11/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 1 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 11, 2014, 2:04:02 PM6/11/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 1 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by RenaudParent):

Ok thanks, I just updated my commit.

--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:13>

Django

unread,
Jun 13, 2014, 12:49:00 PM6/13/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 1
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: 0 => 1

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:14>

Django

unread,
Jun 16, 2014, 5:14:16 AM6/16/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 1
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 16, 2014, 5:26:12 AM6/16/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by RenaudParent):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:16>

Django

unread,
Jun 17, 2014, 9:11:17 AM6/17/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by RenaudParent):

* cc: renaud.parent@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/22778#comment:17>

Django

unread,
Jun 18, 2014, 1:59:48 PM6/18/14
to django-...@googlegroups.com
#22778: Specify default value for a Model's RelatedFields related_name.
-------------------------------------+-------------------------------------
Reporter: renaud.parent@… | Owner: anonymous
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) | Resolution: fixed

Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages