开发者

Django model formset questions

开发者 https://www.devze.com 2022-12-14 21:22 出处:网络
how do you use model formset in Django? When you do this: from django.forms.models import modelformset_factory

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())
0

精彩评论

暂无评论...
验证码 换一张
取 消