Hello,

I'm trying to implement a default relationship between models.

Each otherApp.Partner model can have zero or more Registration models.  
One Registration per otherApp.Partner can be flagged as the default 
(default_reg).

What I would like is for the default_reg boolean to be configurable from the 
Admin.  I've written up the code below.  Save properly updates the other 
Relationships, but the Admin form validation won't allow (unique error) another 
Registration to be set as the default, until the first Registration is 
unflagged.

How do I override the form validation or have it ignore the unique constraint, 
because it is handled in the model save method?

Or is there a better way to do this?


class BasePartnerModel(BaseRFModel):
    """ Adds the partner key to the model. """
    partner = ForeignKey('otherApp.Partner')
    
    class Meta:
        abstract = True


class Registration(BasePartnerModel):
    """ When a user registers with us before registering with a partner, we 
need to send the
    user's information to the partner.  This model tracks where to send the 
registration
    information, and maps the results between the partner and us."""
    # TrueNoneField from http://www.djangosnippets.org/snippets/1831/
    default_reg = 
TrueNoneField(default=None,verbose_name=_('default'),help_text=_('Default 
registration if one is not specifed, if no default, first one is used.'))


    def save(self, force_insert=False, force_update=False):
        if self.default_reg is True:
            # if this is the new default, set others to False
            
Registration.objects.filter(partner=self.partner).exclude(pk=self.pk).update(default_reg=None)
            
        super(Registration, self).save(force_insert, force_update) # Call the 
"real" save() method.
        
    class Meta:
        unique_together = (('partner','default_reg'),)        



--
Eric Chamberlain

--

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


Reply via email to