以下代码来自多供应商电子商务门户网站。我们需要根据结账摘要页面上的商店(或供应商)显示不同的送货方式。
然而,即使我在打印时获得正确的查询集,即Store 1具有Test Rest of World Shipping方法而Store 2具有UPC和DHL,渲染的表单显示的值不正确 -
#########################################################
class ShippingCountryChoiceField(forms.ModelChoiceField):
widget = forms.RadioSelect()
def label_from_instance(self, obj):
price_html = format_price(obj.price.gross, obj.price.currency)
label = mark_safe('%s %s' % (obj.shipping_method, price_html))
return label
class ShippingMethodForm(forms.Form):
def __init__(self, country_code, *args, **kwargs):
stores = kwargs.pop('stores')
super(ShippingMethodForm, self).__init__(*args, **kwargs)
for count, store in enumerate(stores, start=1):
method_field = ShippingCountryChoiceField(
queryset=ShippingMethodCountry.objects.select_related(
'shipping_method').order_by('price').filter(shipping_method__store=store),
label=pgettext_lazy('Shipping method form field label', 'Shipping method for %s' % store),
required=True)
if country_code:
queryset = method_field.queryset
method_field.queryset = queryset.unique_for_country_code(country_code)
if self.initial.get('method') is None:
method_field.initial = method_field.queryset.first()
method_field.empty_label = None
self.fields['method_%d' % count] = method_field
print [q.queryset for q in self.fields.values()]
###################################################
@load_checkout
@validate_voucher
@validate_cart
@validate_is_shipping_required
@validate_shipping_address
@add_voucher_form
def shipping_method_view(request, checkout):
country_code = checkout.shipping_address.country.code
stores = checkout.cart.lines.values_list('variant__product__store', flat=True)
stores = Store.objects.filter(id__in=stores)
print checkout.shipping_method
shipping_method_form = ShippingMethodForm(
country_code, request.POST or None, initial={'method': checkout.shipping_method},
stores=stores)
if shipping_method_form.is_valid():
for count, store in enumerate(stores):
checkout.shipping_method[store] = shipping_method_form.cleaned_data['method_%s' % count]
return redirect('checkout:summary')
print [q.queryset for q in shipping_method_form.fields.values()]
return TemplateResponse(request, 'checkout/shipping_method.html', context={
'shipping_method_form': shipping_method_form, 'checkout': checkout})
##############################################################
{% extends "checkout/details.html" %}
{% load i18n %}
{% load gross from prices_i18n %}
{% load bootstrap_form from bootstrap3 %}
{% block forms %}
<h3>{% trans "Shipping address" context "Checkout shipping address title" %}</h3>
{% include "userprofile/snippets/address-short.html" with address=checkout.shipping_address only %}
<p><a href="{% url 'checkout:shipping-address' %}">{% trans "Select other address" %}</a></p>
<hr>
<form method="post" novalidate>
{% csrf_token %}
{% bootstrap_form shipping_method_form show_label=True %}
<p class="text-md-right">
<button type="submit" class="btn primary">
{% trans "Continue" context "Checkout shipping method primary action" %}
</button>
</p>
</form>
{% endblock %}
答案 0 :(得分:0)
我认为问题在于您在字段定义中实例化窗口小部件。这可能导致状态在不同字段之间共享。尝试将其更改为:
class ShippingCountryChoiceField(forms.ModelChoiceField):
widget = forms.RadioSelect
...