views:

124

answers:

3

Hi,

how do you use model formset in Django? When you do this:

from django.forms.models import modelformset_factory
OrderFormset = modelformset_factory(Order)
formset = OrderFormset()

formset has all Orders from DB...

How do I limit them for example to profile.orders (Profila is connected to Order with FK)?

Thanks in advance, Etam.

+2  A: 

By passing a queryset argument

from django.forms.models import modelformset_factory
OrderFormset = modelformset_factory(Order)
formset = OrderFormset(queryset=Order.objects.filter(name="myorder"))

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-custom-queryset

czarchaic
A: 

Sounds like you want to use inline formsets.

Inline formsets is a small abstraction layer on top of model formsets. These simplify the case of working with related objects via a foreign key.

John Debs
+1  A: 

czarchaic got it right. I am just trying to modify it to your needs exactly:

from django.forms.models import modelformset_factory
OrderFormset = modelformset_factory(Order)
user_profile = request.user.get_profile()
formset = OrderFormset(queryset=user_profile.order_set.all())
Lakshman Prasad