#14018: Introduce class_plural %-substitution placeholder for related_name of
ForeignKey/ManyToManyField
----------------------------------------------------------------------------+
 Reporter:  puxlit                                                          |   
    Owner:  nobody    
   Status:  new                                                             |   
Milestone:  1.3       
Component:  Database layer (models, ORM)                                    |   
  Version:  SVN       
 Keywords:  related_name, ManyToManyField, ForeignKey, abstract base class  |   
    Stage:  Unreviewed
Has_patch:  1                                                               |  
----------------------------------------------------------------------------+
 Django 1.2 [http://docs.djangoproject.com/en/dev/topics/db/models/#be-
 careful-with-related-name introduced] two %-substitution placeholders,
 `%(class)s` and `%(app_label)s`, to work around uniqueness issues when a
 custom `related_name` for a `ForeignKey` or `ManyToManyField` defined in
 an abstract base class is inherited by multiple child classes.

 Quoting from [http://docs.djangoproject.com/en/dev/topics/db/models/#many-
 to-many-relationships Django's models documentation], "''It's suggested
 ![...] that the name of a `ManyToManyField` ![...] be a plural describing
 the set of related model objects.''" Given the nature of a
 `ManyToManyField`, it may be preferable to reciprocate this pluralized
 field naming style through `related_name`. By introducing a third
 %-substitution placeholder, `%(class_plural)s`, this can be accomplished.

 Consider the following snippet:

 {{{
 #!python

 class Tag(models.Model):
     slug = models.SlugField(max_length=63, unique=True)

 class Post(models.Model):
     slug = models.SlugField(max_length=127, unique=True)
     tags = models.ManyToManyField(Tag, related_name='%(class_plural)s') #
 docs suggest related_name='%(app_label)s_%(class)s_related'

     class Meta:
         abstract = True

 class Entry(Post):
     title = models.CharField(max_length=255)
     content = models.TextField()

     class Meta:
         verbose_name_plural = 'entries'

 class Event(Post):
     summary = models.CharField(max_length=255)
     start_date = models.DateTimeField()
 }}}

 Arguably, `tag.entries` and `tag.events` are more semantic than either
 `tag.entry_set` and `tag.event_set` or `tag.appname_entry_related` and
 `tag.appname_event_related`.

 Ultimately, the introduction of a `%(class_plural)s` %-substitution
 placeholder provides greater flexibility when designing models with
 abstract base classes and ForeignKeys or ManyToManyFields.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to