Dnia 11-03-2011 o 18:06:43 greenie2600 <[email protected]> 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?
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
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to [email protected].
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.