edit: you don't need to raise an error if the restaurant already has 3
Cuisine's objects associated, you just need to return a string with
the explanation of the error.

On 11 mar, 20:11, gontran <geoffroydecorb...@gmail.com> wrote:
> Hi greenie,
>
> you just need to override the save method from your model Restaurant.
> Before saving each instance, you check if the restaurant already  has
> 3 Cuisine's objects associated. If yes, you don't save the instance
> and raise an error for exemple, if no, just call the standard method
> of the super class Model.
> You will find more infos in the django 
> documentation:http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-pre...
>
> On 11 mar, 18:06, greenie2600 <greenie2...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hi all -
>
> > I have two models with a many-to-many relationship: Restaurant and
> > Cuisine. The Cuisine table contains, e.g., "Italian", "Mexican",
> > "Chinese", etc. Each Restaurant record can be associated with one or
> > more Cuisines.
>
> > Here's the thing: I'd like to limit this to three Cuisines per
> > Restaurant. So when editing the record for "Bob's Pan-Asian Buffet",
> > the user would be able to check "Japanese", "Chinese", and "Korean",
> > but wouldn't be able to check a fourth box.
>
> > My question: can this be enforced within the model, or is this
> > something I'd have to build into my interface layer?
>
> > Here's my models.py.
>
> > from django.db import models
> > from django.contrib.auth.models import User
>
> > class Restaurant( models.Model ):
>
> >     user = models.ForeignKey( User )
> >     name = models.CharField( max_length = 128 )
> >     slug = models.CharField( max_length = 24, unique = True )
> >     cuisines = models.ManyToManyField( 'Cuisine' )
>
> >     def __unicode__(self):
> >         return self.name
>
> > class Cuisine( models.Model ):
>
> >     name = models.CharField( max_length = 32 )
>
> >     def __unicode__(self):
> >         return self.name
>
> >     class Meta:
> >         ordering = ['name']

-- 
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 
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