Django model formset questions
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 exa开发者_开发百科mple to profile.orders (Profila is connected to Order with FK)?
Thanks in advance, Etam.
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
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.
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())
精彩评论