Opened 10 years ago

Closed 10 years ago

#22778 closed New feature (fixed)

Specify default value for a Model's RelatedFields related_name.

Reported by: renaud.parent@… 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 Renaud Parent, 10 years ago

comment:2 by Renaud Parent, 10 years ago

Owner: changed from nobody to anonymous
Status: newassigned
Version: 1.6master

comment:4 by Aymeric Augustin, 10 years ago

Needs documentation: set
Triage Stage: UnreviewedAccepted

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:5 by Renaud Parent, 10 years ago

I have started a working branch for this ticket, it is available here :

https://github.com/RenaudParent/django/tree/ticket_22778

comment:6 by Renaud Parent, 10 years ago

I have submitted a pull request at https://github.com/django/django/pull/2785
All tests pass under sqlite

comment:7 by Tim Graham, 10 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 jorgecarleitao, 10 years ago

Cc: jorgecarleitao@… 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:

  1. 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].
  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 to Meta, the relations without related_name get the default one, while others don't. We may also want to test subclassing.
  1. 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.)

  1. There should be an item in docs/releases/1.X.rst (X=8?) describing this new functionality.
  1. 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

Last edited 10 years ago by jorgecarleitao (previous) (diff)

comment:9 by Renaud Parent, 10 years ago

I have pushed a new commit based on previous comments here : https://github.com/RenaudParent/django/commit/8710030d4c52b1c542e735fdf026fdcd0842518a

comment:10 by Renaud Parent, 10 years ago

Patch needs improvement: unset

comment:11 by Tim Graham <timograham@…>, 10 years ago

In 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.

comment:12 by Tim Graham <timograham@…>, 10 years ago

In 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

comment:13 by Renaud Parent, 10 years ago

Ok thanks, I just updated my commit.

comment:14 by Tim Graham, 10 years ago

Needs tests: unset
Patch needs improvement: set

comment:16 by Renaud Parent, 10 years ago

Patch needs improvement: unset

comment:17 by Renaud Parent, 10 years ago

Cc: renaud.parent@… added

comment:18 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 87d0a3384cc263fe0df749a28e2fbc1e1240f043:

Fixed #22778 -- Added a model Meta option to define default_related_name.

Thanks jorgecarleitao and mmardini for reviews.

Note: See TracTickets for help on using tickets.
Back to Top