Changes between Version 2 and Version 3 of CookBookPredicates
- Timestamp:
- Jun 26, 2006, 11:14:07 PM (18 years ago)
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:1 The [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. 2 2 3 3 {{{ … … 5 5 6 6 class Individual (models.Model): 7 #...8 7 class Admin: pass 9 8 10 9 class Event (models.Model): 11 #...12 10 class Admin: pass 13 11 14 12 class Participation (models.Model): 15 # leave defaults: null=False , core=False16 13 event = models.ForeignKey(Event, related_name='participants', edit_inline=True, blank=True, default=None) 17 14 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)19 15 role = models.PositiveSmallIntegerField(core=True, blank=True, default=None) 20 16 class Admin: pass 21 17 }}} 22 18 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.''