Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django FormView Not Saving

My form isn't saving the models that I need it to. My form:

class RewardForm(forms.Form):
    quantity = forms.IntegerField(max_value=10, min_value=1, label=_('quantity'), initial=1)
    reward = forms.CharField(max_length=50, label=_('reward'))
    reward_denomination = forms.ModelChoiceField(queryset=Reward_Denomination.objects.all(), widget=forms.RadioSelect)

    def clean_reward(self):
        data = self.cleaned_data.get('reward')
        try:
            reward = Reward.objects.get(reward_name=data)
        except ObjectDoesNotExist:
            raise forms.ValidationError(_('Reward does not exist'), code='invalid')
        return data

    def clean_reward_denomination(self):
        data = self.cleaned_data.get('reward_denomination')
        try:
            denomination = Reward_Denomination.objects.get(denomination=data)
        except ObjectDoesNotExist:
            raise forms.ValidationError(_('Denomination does not exist'), code='invalid')
        return data

    def save(self, request, commit=True):
        user = request.user
        data = self.cleaned_data

        'try:
            post_reward = data['reward']
            post_denomination = data['reward_denomination']
            quantity = data['quantity']
        except LookupError:
            raise Http404

        reward = Reward.objects.get(reward_name=post_reward)
        denomination = Reward_Denomination.objects.get(denomination=post_denomination)
        user_points = Points.objects.filter(affiliate__id=user.id).aggregate(total_points=Sum('points'))
        user_points = user_points['total_points']

        try:
            total_cost = (quantity * denomination.cost)
        except ArithmeticError:
            raise Http404

        quote_price = -total_cost

        if user_points >= total_cost:
                reward_order = Points.objects.create(affiliate=user, points=quote_price, from_reward=True, from_offer=False)
                status_coded = Status_Code.objects.create(short_name="Pending", name="The order is currently being reviewed", description="The order is in queue")
                redeem_order = Redeem.objects.create(affiliate=user, status_code=status_coded, quantity=quantity, reward=reward, price=total_cost)

        return reward_order

My Views:

class Reward_Detail(DetailView):
    model = Reward
    slug_field = 'reward_slug'
    context_object_name = 'reward'
    template_name = 'omninectar/reward.html'


#Detail Stuff

class RedeemReward(SingleObjectMixin, FormView):
    template_name = 'omninectar/reward.html'
    slug_field = 'reward_slug'
    form_class = RewardForm
    model = Reward

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super(RedeemReward, self).post(request, *args, **kwargs)

    def get_success_url(self):
        return reverse('omni:reward_confirmation')

class RewardBeautify(View):
    def get(self, request, *args, **kwargs):
        view = Reward_Detail.as_view()
        return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        view = RedeemReward.as_view()
        return view(request, *args, **kwargs)

So I initially thought that the FormView would handle the form processing (validate, and, if valid, run form.save(), etc). I'm following the FormView, SingleObjectMixin example on the Django website. I don't receive any errors when I try and submit the form, but no objects are created either. I've tried defining a form_valid method that runs the save method, I've tried putting it inside the post method in the formview, etc. Can anyone spot the error/errors? Thanks!

like image 355
user3084860 Avatar asked Dec 31 '13 06:12

user3084860


1 Answers

I'm new to view classes too and I had almost the same problem with Django 1.6.

You should add

def form_valid(self, form):
    form.save()
    return super(RedeemReward, self).form_valid(form)

method overriding to your RedeemReward class. This worked for me.

If you look to Django source code, you will see that there is no form saving in FormView class inheritance chain.

like image 176
dojy Avatar answered Oct 20 '22 17:10

dojy