我的Django表单应该创建并将几个不同的模型实例保存到数据库中。但是,当我检查管理员时,没有任何关于这种情况的提示。我一直在调整表格,但似乎没用。理想情况下,此表单将检查用户是否有足够的积分用于他想要的奖励,如果他有,则会创建必要的对象并扣除必要的积分。但是,虽然我进入了成功页面,但没有创建任何对象并将其保存到数据库中。
形式:
class Get_Reward(forms.Form):
quantity = forms.IntegerField(label=_('Quantity'), error_messages={'invalid':'Must be a valid number'})
reward_name = forms.CharField(max_length=50, widget=forms.HiddenInput(), label=_('Reward Name'), error_messages={'invalid':'Invalid reward'})
def clean_quantity(self):
return self.cleaned_data['quantity']
def clean_reward_name(self):
try:
existing_reward = Reward.objects.get(reward_name=self.cleaned_data['reward_name'])
except ObjectDoesNotExist:
raise forms.ValidationError(_("The reward you requested does not exist."))
return self.cleaned_data['reward_name']
def save(self, request, *args, **kwargs):
"""
Save all of the required data.
"""
user = request.user
user_points = Points.object.filter(affiliate__id=user.id).annotate(total_points=Sum('points'))
user_points = user_points.total_points
error_message = {'lookuperror':'You need to provide a valid quantity',
'insufficient_points':"You don't have enough points for this purchase."}
form = Get_Rewards(request.POST)
try:
quantity = form.cleaned_data['quantity']
reward_name = form.cleaned_data['reward_name']
rewards = Reward.objects.get(reward_name=reward_name)
except LookupError:
raise Http404
try:
points_cost = -(rewards.reward_cost * quantity)
except ArithmeticError:
raise Http404
quote_price = -(points_cost)
if user_points >= quote_price:
reward_order = Points.objects.create(affiliate=user, reward=rewards, points=points_cost, from_reward=True, from_offer=False, from_referral=False)
status_cost = 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, reward=rewards, status_code=status_code)
redeem_details = Redeem_Details.objects.create(redeem=redeem_order, quantity=quantity, quote_price=quote_price)
"""
reward_order = Points(affiliate=user, reward=reward, points=points_cost, from_reward=True, from_offer=False, from_referral=False)
reward_order.save()
status_code = Status_Code(short_name="Pending", name="The order is currently being reviewed", description="The order is in queue")
status_code.save()
redeem_order = Redeem(affiliate=user, reward=rewards, status_code=status_code)
redeem_order.save()
redeem_details = Redeem_Details(redeem=redeem_order, quantity=quantity, quote_price=quote_price)
redeem_details.save()
"""
return HttpResponseRedirect(reverse('yum:reward_confirmation', args=(redeem_details.redeem_code,)))
else:
return render(request, 'yum_affiliates/reward.html', {'error_message':error_message['insufficient_points']})
查看:
class RedeemReward(SingleObjectMixin, FormView):
template_name = 'yum_affiliates/reward.html'
slug_field = 'reward_slug'
form_class = Get_Reward
model = Reward
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return HttpResponseForbidden()
self.object = self.get_object()
return super(RedeemReward, self).post(request, *args, **kwargs)
def get_success_url(self):
return reverse('yum:reward_confirmation', args=(self.object.reward_slug, self.object.reward_code))