Re: Many to One - Requriring 2 fields to be different
Thank you Rajesh, your advice gave me the hints I needed. Much easier than I thought it would be. class FriendshipAdminForm(forms.ModelForm): class Meta: model = Friendship def clean_to_friend(self): if 'to_friend' in self.cleaned_data: from_friend = self.cleaned_data['from_friend'] to_friend = self.cleaned_data['to_friend'] if from_friend == to_friend: raise forms.ValidationError('Please select two different friends.') return to_friend On Nov 19, 11:18 am, Rajesh Dhawan <[EMAIL PROTECTED]> wrote: > kevinski wrote: > > I'm currently going through the "Learning Website Dev with Django" > > book, using Django 1.0.2/Python 2.5/SQLite. There is an issue stumping > > me. The book includes a "friendship" model which includes 2 foreign > > keys set to User model: > > > class Friendship(models.Model): > > from_friend = models.ForeignKey( > > User, related_name='friend_set' > > ) > > to_friend = models.ForeignKey( > > User, related_name='to_friend_set' > > ) > > def __unicode__(self): > > return '%s, %s' % ( > > self.from_friend.username, > > self.to_friend.username > > ) > > class Meta: > > unique_together = (('to_friend', 'from_friend'), ) > > > In the admin interface, it is possible to set a user to be his own > > friend, however I don't want this to be allowed. What is the best > > practice for prohibititing a record from allowing the same value in 2 > > fields? I've read through all the documentation, but found nothing. Is > > this possible in models.py, or do I need to hack the admin form to > > prohibit this relationship from occuring in the admin interface? > > Yes, the ideal way to do this is to use a custom model form that > implements your validation. Incidentally, this is not a hack because > the admin supports this behaviour using the well-documented form > attribute on your admin class. > > The custom model form can then also be used in other non-admin views > if you ever need to. Thus, you have one place where that validation > takes place consistently. > > -Rajesh D --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Many to One - Requriring 2 fields to be different
kevinski wrote: > I'm currently going through the "Learning Website Dev with Django" > book, using Django 1.0.2/Python 2.5/SQLite. There is an issue stumping > me. The book includes a "friendship" model which includes 2 foreign > keys set to User model: > > class Friendship(models.Model): > from_friend = models.ForeignKey( > User, related_name='friend_set' > ) > to_friend = models.ForeignKey( > User, related_name='to_friend_set' > ) > def __unicode__(self): > return '%s, %s' % ( > self.from_friend.username, > self.to_friend.username > ) > class Meta: > unique_together = (('to_friend', 'from_friend'), ) > > In the admin interface, it is possible to set a user to be his own > friend, however I don't want this to be allowed. What is the best > practice for prohibititing a record from allowing the same value in 2 > fields? I've read through all the documentation, but found nothing. Is > this possible in models.py, or do I need to hack the admin form to > prohibit this relationship from occuring in the admin interface? Yes, the ideal way to do this is to use a custom model form that implements your validation. Incidentally, this is not a hack because the admin supports this behaviour using the well-documented form attribute on your admin class. The custom model form can then also be used in other non-admin views if you ever need to. Thus, you have one place where that validation takes place consistently. -Rajesh D --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Many to One - Requriring 2 fields to be different
I'm currently going through the "Learning Website Dev with Django" book, using Django 1.0.2/Python 2.5/SQLite. There is an issue stumping me. The book includes a "friendship" model which includes 2 foreign keys set to User model: class Friendship(models.Model): from_friend = models.ForeignKey( User, related_name='friend_set' ) to_friend = models.ForeignKey( User, related_name='to_friend_set' ) def __unicode__(self): return '%s, %s' % ( self.from_friend.username, self.to_friend.username ) class Meta: unique_together = (('to_friend', 'from_friend'), ) In the admin interface, it is possible to set a user to be his own friend, however I don't want this to be allowed. What is the best practice for prohibititing a record from allowing the same value in 2 fields? I've read through all the documentation, but found nothing. Is this possible in models.py, or do I need to hack the admin form to prohibit this relationship from occuring in the admin interface? Thanks, Kevin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---