2017-07-11 9 views
1

次のコードはマルチベンダーのeコマースポータルのコードです。チェックアウトの概要ページに店舗(または仕入先)ごとに異なる配送方法を表示する必要があります。私は問題を信じpage screenshotDjangoフォームフィールドに正しいクエリーセット値が表示されない

######################################################### 
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

正しい値は何ですか? – Exprator

+0

私はあなたの答えの質問を編集しました - Store 1にはTest Out of World Shippingメソッドがあり、Store 2にはUPCとDHLがあります – iamkhush

答えて

0

- 印刷すなわちストア1は、世界の発送方法のテスト休息とストア2は、UPCとDHLは、レンダリングされたフォームは、不正な値を示していた間、私は正しいクエリセットを取得するにもかかわらず、しかし

フィールド定義でウィジェットをインスタンス化しているということです。これにより、異なるフィールド間で状態が共有される可能性があります。変更してみてください:

class ShippingCountryChoiceField(forms.ModelChoiceField): 

    widget = forms.RadioSelect 
    ... 
+0

ありがとう@alasdair – iamkhush

関連する問題