简体   繁体   中英

Django filter a ModelChoiceField queryset using the URL to populate a form

I'm sure this is either a simple fix or I fundamentally misunderstand something.

In this application the Stores are the "products" and there are several variants of Bonds available for each store.

The desired behavior is essentially:

  1. User is directed to website.com/stores/store-slug
  2. Filter for matching Store Store.objects.filter(anchor=store_slug)
  3. Form choices are populated with Bonds.objects.filter(store=store)

I want to populate the ListingForm's choices with the Bonds associated with that Store. Can someone help me achieve this properly?

urls.py

urlpatterns = [
    path('', views.HomeView.as_view(), name='home'),
    path('stores/<slug:anchor>', views.ListingView.as_view(), name='listing'),
]

forms.py

from django import forms
from store.models import Bond

class ListingForm(forms.Form):
    face_value = forms.ModelChoiceField(queryset=...) # this part is a problem

    def __init__(self, store, *args, **kwargs):
        super(ListingForm, self).__init__(*args, **kwargs)
        self.fields['face_value'].queryset = Bond.objects.filter(store=store)

view.py

class ListingView(FormView):
    template_name = 'listing.html'
    form_class = ListingForm
    success_url = '/checkout/preview' # todo

    anchor = None # this feels wrong...
    store = None
    queryset = None

    def get_form_kwargs(self):
        kwargs = super(ListingView, self).get_form_kwargs()
        kwargs['store'] = self.store

    def get_context_data(self, **kwargs):
        self.anchor = self.kwargs.get('anchor')
        self.store = Store.objects.filter(anchor_id=self.anchor).first()
        self.queryset = Bond.objects.filter(store=self.store)

        context = super(FormView, self).get_context_data(**kwargs)

        context['store'] = self.store
        context['store_bonds'] = self.queryset
        return context

    def form_valid(self, form):
        # save selected bond to session
        # redirect to success_url
        face_value = form.cleaned_data['face_value']
        bond = Bond.objects.filter(store=self.store, face_value=face_value).first()

        if bond:
            self.request.session['bond'] = bond
            return super().form_valid(form)

You can overwrite it manually in the view, where you'll have the store_slug:

class ListingView(FormView):

    def get_form(self, form_class=None):
        store_slug = self.kwargs['store_slug']
        form = ListingForm()
        form.fields['face_value'].queryset = Store.objects.filter(anchor=store_slug)
        return form

while just have a placeholder in the form

class ListingForm(forms.Form):
    face_value = forms.ModelChoiceField(queryset=Store.objects.all())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM