Version 5 (modified by SergKovrov, 16 years ago) (diff)

updated link to source file in repository

Many to Many Intermediary Tables in Admin

The [source:django/trunk/tests/modeltests/m2m_intermediary/ Many-to-Many Intermediary] test case already shows how this is structured. What I add here is a set of parameters for the fields in the intermediary model that allow smooth inline editing in admin.

from django.db import models

class Individual (models.Model):
    class Admin: pass

class Event (models.Model):
    class Admin: pass

class Participation (models.Model):
    event = models.ForeignKey(Event, related_name='participants', edit_inline=True, blank=True, default=None)
    individual = models.ForeignKey(Individual, related_name='events', edit_inline=True, blank=True, default=None)
    role = models.PositiveSmallIntegerField(core=True, blank=True, default=None)
    class Admin: pass
  1. edit_inline=True
  2. core=False # So that when an Individual is being created along with a few Participations created inline, the Participation records will be valid even though the Individual doesn't fully exist yet.
  3. blank=True, default=None # So that untouched inline records will have blank core values and thus will not be created.
Back to Top