Clean Method for a ModelForm in a ModelFormSet made by modelformset_factory

Posted by Salyangoz on Stack Overflow See other posts from Stack Overflow or by Salyangoz
Published on 2013-11-07T03:49:01Z Indexed on 2013/11/07 3:53 UTC
Read the original article Hit count: 255

Filed under:
|
|
|
|

I was wondering if my approach is right or not. Assuming the Restaurant model has only a name.

forms.py

class BaseRestaurantOpinionForm(forms.ModelForm):
opinion = forms.ChoiceField(choices=(('yes', 'yes'), ('no', 'no'), ('meh', 'meh')),
                            required=False,
                            ))
class Meta:
    model = Restaurant
    fields = ['opinion']

views.py

class RestaurantVoteListView(ListView):
    queryset = Restaurant.objects.all()
    template_name = "restaurants/list.html"

    def dispatch(self, request, *args, **kwargs):
        if request.POST:
            queryset = self.request.POST.dict()
        #clean here
            return HttpResponse(json.dumps(queryset), content_type="application/json")

    def get_context_data(self, **kwargs):
        context = super(EligibleRestaurantsListView, self).get_context_data(**kwargs)
        RestaurantFormSet = modelformset_factory(
                                Restaurant,form=BaseRestaurantOpinionForm
                            )
        extra_context = {
            'eligible_restaurants' : self.get_eligible_restaurants(),
            'forms' : RestaurantFormSet(),
        }
        context.update(extra_context)
        return context

Basically I'll be getting 3 voting buttons for each restaurant and then I want to read the votes. I was wondering from where/which clean function do I need to call to get something like:

{ ('3' : 'yes'), ('2' : 'no') } #{ 'restaurant_id' : 'vote' }

This is my second/third question so tell me if I'm being unclear. Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about django