如何将表单中payment_mode的值保存到PaymentChoices模型?
models.py
9 PAYMENT_MODE=(('Credit Card','Credit Card'),('Bank Transfer','Bank Transfer'),
10 ('Bankdraft/Cheque','Bankdraft/Cheque'),
11 ('Other','Other'))
12
13 class PaymentChoices(models.Model):
14 currency = models.CharField('Payment Modes', choices=PAYMENT_MODE, max_length=25)
15
16 class EventPaymentDetail(models.Model):
17 event = models.ForeignKey(Event)
18 merchant_name = models.CharField('Merchant Name', max_length=50, blank=True, null=True, default='INMEET CMS PTE LTD')
19 currency = models.CharField('Currency', choices=CURRENCY_LIST, default='SGD', max_length=3)
20 payment_mode = models.ManyToManyField(PaymentChoices)
21 transaction_prefix = models.CharField(max_length=5, blank=True, null=True)
22 tax = models.DecimalField('Tax', decimal_places=2, max_digits=10, default=0)
23 terms = models.CharField('Terms and Conditions', max_length=2500, blank=True, null=True)
24 modified = models.DateTimeField(auto_now=True, editable=False)
25 created = models.DateTimeField(default=datetime.datetime.now, editable=False)�editable=False)
forms.py
9 class EventPaymentDetailForm(forms.ModelForm):
10 account_name = forms.CharField(label='Account Name', max_length=100, required=False)
11 bank_name = forms.CharField(label='Bank Name', max_length=100, required=False)
12 branch_name = forms.CharField(label='Branch Name', max_length=100, required=False)
13 address = forms.CharField(label='Bank Address' ,max_length=100, required=False)
14 account_number = forms.CharField(label='Account Number', max_length=50, required=False)
15 payment_mode = forms.MultipleChoiceField(label='Payment Mode', widget=CheckboxSelectMultiple(), choices=PAYMENT_MODE)
16
17 class Meta:
18 model = EventPaymentDetail
19 fields = ('merchant_name',
20 'currency',
21 'payment_mode',
22 'account_name',
23 'account_number',
24 'bank_name',
25 'branch_name',
26 'address',
27 'transaction_prefix',
28 'tax',
29 'terms',)
30 widgets = {'terms': forms.Textarea }
views.py
20 @login_required
21 @event_admin_only
22 def payment_details(request, event_slug, role_id, module_slug):
23 event = get_object_or_404(Event, slug=event_slug)
24 payment_details = EventPaymentDetail.objects.get_or_create(event=event)[0]
25 bank_details = EventBankAccountDetail.objects.get_or_create(event=event)[0]
26
27 if request.method == 'POST':
28 payment_form = EventPaymentDetailForm(request.POST, instance=payment_details)
29 display_error(request, payment_form)
30 if payment_form.is_valid():
31
32 cd = payment_form.cleaned_data
33 bank_details.account_number = cd['account_number']
34 bank_details.bank_name = cd['bank_name']
35 bank_details.address = cd['address']
36 bank_details.account_name = cd['account_name']
37 bank_details.branch_name = cd['branch_name']
38 bank_details.save()
# stuck here ???
40 payment_details = payment_form.save()
42
43 messages.success(request, 'Payment details successfully saved!')
44 url = reverse('registration_item_details', args=[event_slug, role_id, module_slug])
45 return redirect(url)
46
47 else:
48 initial_values={'account_number': bank_details.account_number,
49 'account_name': bank_details.account_name,
50 'bank_name': bank_details.bank_name,
51 'address': bank_details.address,
52 'branch_name': bank_details.branch_name}
53 payment_form = EventPaymentDetailForm(initial=initial_values, instance=payment_details)
54
55 template = 'registration/payment_details.html'
56 return render(request, template, locals())
request.POST
{'bank_name': u'', 'terms': u'tesfefsef', 'branch_name': u'', 'tax': Decimal('0.10'), 'transaction_prefix': u'234', 'currency': u'SGD', 'account_number': '', 'address': u'', 'payment_mode': [u'Credit Card', u'Bank Transfer', u'Bankdraft/Cheque', u'Other'], 'account_name': u'', 'merchant_name': u'INMEET CMS PTE LTD'}
答案 0 :(得分:0)
答案是使用ModelMultipleChoiceField
,如此:
payment_mode = forms.ModelMultipleChoiceField(label='Payment Mode', widget=CheckboxSelectMultiple(), queryset=PaymentChoices.objects.all())
模型中的初始值将为空,因此您必须使用选项列表中的值填充它。