Code

Changes between Version 2 and Version 3 of CookBookPredicates


Ignore:
Timestamp:
06/26/06 21:14:07 (8 years ago)
Author:
Joshua Tacoma
Comment:

added more explanation

Legend:

Unmodified
Added
Removed
Modified
  • CookBookPredicates

    v2 v3  
    1 The straight-forward way to do this with a relational database is to create an intermediary table (as Django does automatically with ManyToManyField) and then add fields to that table.  Setting up this structure in Django is also straight-forward, but it took some work for me to find the right Field arguments to make it all work as I'd like on the admin side.  Here's how I did it: 
     1The [source:trunk/tests/testapp/models/m2m_intermediary.py 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. 
    22 
    33{{{ 
     
    55 
    66class Individual (models.Model): 
    7     #... 
    87    class Admin: pass 
    98 
    109class Event (models.Model): 
    11     #... 
    1210    class Admin: pass 
    1311 
    1412class Participation (models.Model): 
    15     # leave defaults: null=False , core=False 
    1613    event = models.ForeignKey(Event, related_name='participants', edit_inline=True, blank=True, default=None) 
    1714    individual = models.ForeignKey(Individual, related_name='events', edit_inline=True, blank=True, default=None) 
    18     # set core=True and blank=True: now admin will show it blank by default (so it won't exist unless you specify something) 
    1915    role = models.PositiveSmallIntegerField(core=True, blank=True, default=None) 
    2016    class Admin: pass 
    2117}}} 
    2218 
     19 1. {{{edit_inline=True}}} 
     20 1. {{{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.'' 
     21 1. {{{blank=True, default=None}}} ''# So that untouched inline records will have blank core values and thus will not be created.''