> 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?
>
You can limit choices on model level:
http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.ForeignKey.limit_choices_to
If query is too complicated (cuz u want to access object's data, witch
isn't yet available), you still can limit choices on form level, by
overriding __init__
class RestaurantForm(forms.ModelForm):
cuisines = forms.ModelMultipleChoiceField(Sklep)
class Meta:
model = Restaurant
def __init__(self, *args, **kwargs):
super(RestaurantForm, self).__init__(*args, **kwargs)
self.fields[cuisines].queryset =
Cuisine.objects.filter(pk__in=[fancy query])
--
Linux user
Right, i just get to that point, there is a cave rat about limiting
choices on form level.
Depending on limiting query it may make problems if you edit this object.
afik U can't validate m2m fields on model level, but u can do it on form
level and rise forms.ValidationError if needed in clean_field().
--
Linux user
> bagheera -
>
> I had seen the limit_choices_to parameter, but I thought it controlled
> *which* choices are available to the user - not *how many* they're
> allowed to choose.
>
> I want to show the user a list of 20 or 30 cuisines, but forbid them
> from checking more than three.
>
> Can you show me an example of how I'd use limit_choices_to to limit
> the *number* of choices the user can select?
>
Form validation.
this should work
class RestaurantForm(forms.ModelForm):
cuisines = forms.ModelMultipleChoiceField(Sklep)
class Meta:
model = Restaurant
def clean_sklepy(self):
cuisines_clean = self.cleaned_data[cuisines]
if len(cuisines_clean) > 3:
raise forms.ValidationError('You can't choose more than
three items!')
return cuisines_clean
--
Linux user
> bagheera -
>
> I had seen the limit_choices_to parameter, but I thought it controlled
> *which* choices are available to the user - not *how many* they're
> allowed to choose.
>
> I want to show the user a list of 20 or 30 cuisines, but forbid them
> from checking more than three.
>
> Can you show me an example of how I'd use limit_choices_to to limit
> the *number* of choices the user can select?
>
>
>
> On Mar 11, 2:35 pm, bagheera <neost...@go2.pl> wrote:
>> Dnia 11-03-2011 o 18:06:43 greenie2600 <greenie2...@gmail.com>
>> napisał(a):
>>
>> > 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?
>>
or u can add a js script to this form field that will disallow selecting
more than three items instead validating model (or do both).
--
Linux user
Sorry, i left some of my code :P But u got the idea.
--
Linux user
> 'Restaurant' instance needs to have a primary key value before a many-
> to-many relationship can be used.
That is the answer You need to understand. afiak You can't perform m2m
validation on model level due that very reason.
That's why i did it on form level, and i think that's the only way to do
this. I know such things should be performed on server-side and model
level, but i this case, is validation on form level causing any problems?
In my project i use only admin interface, so for end-user that validation
is completely transparent.
--
Linux user