Skip to content

Commit

Permalink
Added optional rel_name parameter to ManyToManyField, which makes it …
Browse files Browse the repository at this point in the history
…possible to many-to-many-relate a single model to another model more than once. Also updated the model docs to reflect this

git-svn-id: http://code.djangoproject.com/svn/django/trunk@257 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Jul 20, 2005
1 parent 895d232 commit bc5359f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
5 changes: 3 additions & 2 deletions django/core/meta.py
Expand Up @@ -2049,10 +2049,11 @@ def get_manipulator_field_objs(self):
return [formfields.IntegerField] return [formfields.IntegerField]


class ManyToManyField(Field): class ManyToManyField(Field):
def __init__(self, to, **kwargs): def __init__(self, to, rel_name=None, **kwargs):
kwargs['name'] = kwargs.get('name', to._meta.module_name) kwargs['name'] = kwargs.get('name', to._meta.module_name)
kwargs['verbose_name'] = kwargs.get('verbose_name', to._meta.verbose_name_plural) kwargs['verbose_name'] = kwargs.get('verbose_name', to._meta.verbose_name_plural)
kwargs['rel'] = ManyToMany(to, to._meta.object_name.lower(), rel_name = rel_name or to._meta.object_name.lower()
kwargs['rel'] = ManyToMany(to, rel_name,
num_in_admin=kwargs.pop('num_in_admin', 0), num_in_admin=kwargs.pop('num_in_admin', 0),
related_name=kwargs.pop('related_name', None), related_name=kwargs.pop('related_name', None),
filter_interface=kwargs.pop('filter_interface', None), filter_interface=kwargs.pop('filter_interface', None),
Expand Down
23 changes: 20 additions & 3 deletions docs/model-api.txt
Expand Up @@ -455,12 +455,17 @@ Field Types


Many-to-many relations are a bit different from other fields. First, they Many-to-many relations are a bit different from other fields. First, they
aren't actually a field per se, because they use a intermediary join table. aren't actually a field per se, because they use a intermediary join table.
Second, they don't take any of the same options as the rest of the fields. Second, they don't take the same options as the rest of the fields. The
The only arguments taken are: only arguments taken are:


======================= ============================================================ ======================= ============================================================
Argument Description Argument Description
======================= ============================================================ ======================= ============================================================
``rel_name`` Use this if you have more than one
``ManyToOneField`` s in the same model that relate
to the same model. Django will use ``rel_name`` in
the generated API.

``related_name`` See the description of ``related_name`` in ``related_name`` See the description of ``related_name`` in
``ManyToOneField``, above. ``ManyToOneField``, above.


Expand All @@ -471,7 +476,19 @@ Field Types
should the interface be stacked horizontally or should the interface be stacked horizontally or
vertically). vertically).


``limit_choices_to`` See the description under ``ManyToOneField``, above. ``limit_choices_to`` See the description under ``ManyToOneField`` above.

``name`` An alphanumeric name for the relationship. If this
isn't provided, Django uses the ``module_name`` of
the related object.

This is only really useful when you have a single
object that relates to the same object more than
once.

``verbose_name`` A human-readable name for the object, singular. If
this isn't provided, Django uses the
``verbose_name`` for the related object.
======================= ============================================================ ======================= ============================================================


``NullBooleanField`` ``NullBooleanField``
Expand Down

0 comments on commit bc5359f

Please sign in to comment.