{"id":158,"date":"2010-08-21T22:44:20","date_gmt":"2010-08-22T02:44:20","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=158"},"modified":"2010-08-21T22:45:55","modified_gmt":"2010-08-22T02:45:55","slug":"django-custom-queryset-with-a-formset","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2010\/08\/django-custom-queryset-with-a-formset\/","title":{"rendered":"Django &#8211; Custom Queryset with a Formset"},"content":{"rendered":"<p>My goal was to apply a custom queryset to a foreign key field.  When the field is in a single model form, it was pretty clear to me how to proceed.  However, when the field in question is part of a ModelFormset, I wasn&#8217;t seeing the way.  Oddly enough, it came to me right before bed one night &#8211; iterate through the forms in the formset to apply the queryset to each one.  Here&#8217;s how I handled each case.<\/p>\n<p>In this application, there is a one-to-many relationship between Event and Rsvp, and a one-to-many between Rsvp and Seat<\/p>\n<p>(snippet from views.py)<\/p>\n<pre>    ## build the querysets\r\n    event_queryset = Event.futures.filter(institution=inst_id)\r\n    person_queryset = Person.objects.filter(institution=inst_id)    \r\n\r\n    ## create the formset\r\n    SeatFormSet    = inlineformset_factory(Rsvp, Seat, form=SeatForm, can_delete=True, extra=3)\r\n\r\n    ...\r\n   \r\n    ## instantiate the form and formset\r\n    rsvpform      = RsvpForm(instance=rsvp)\r\n    seatformset    = SeatFormSet(instance=rsvp)\r\n\r\n    ## Apply the queryset to the master form\r\n    rsvpform.fields['event'].queryset = event_queryset\r\n\r\n    ## Apply the queryset to each form in the formset\r\n    for form in seatformset.forms:\r\n        form.fields['person'].queryset = person_queryset<\/pre>\n<p>It&#8217;s important to note that the querysets must be applied each time the form and formset are instantiated in your view code in order for the validation to work correctly.<\/p>\n<p>In the for statement, I first tried iterating on the formset itself, but I was told that it was not iterable.  A little perusal of the Django code for formsets showed me that the forms variable in the model was the object to use instead.<\/p>\n<p>I&#8217;d love to hear your comments on this technique.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My goal was to apply a custom queryset to a foreign key field. When the field is in a single model form, it was pretty clear to me how to proceed. However, when the field in question is part of a ModelFormset, I wasn&#8217;t seeing the way. Oddly enough, it came to me right before &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2010\/08\/django-custom-queryset-with-a-formset\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Django &#8211; Custom Queryset with a Formset&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":3,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions\/161"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}