как редактировать форму с одним или несколькими полями Multichoice - PullRequest
1 голос
/ 15 октября 2019
I have a form with MultipleChoiceField. I am able to save the data correctly. Now, if a user wants to edit that form he/she should see already selected items in the dropdown along with remaining all the other option. I want this as a function-based view. eg. I have 5 products in the dropdown and at the time of form submission, I selected products 1 and 2. Now, when I click on edit I should be able to see products 1 and 2 selected along with the other 3 products as unselected.Just help me with the edit view of this form. I am using same template for create and update. the code is messy.

models.py

    class Lead(models.Model):
        state = models.CharField(_("State"), max_length=255, blank=True, null=True)
        type = models.CharField(max_length=20,choices=TYPE,blank=True,null=True)
        products = models.ManyToManyField(Product,related_name='company_products',limit_choices_to=5,blank=True,null=True)

forms.py моя настраиваемая форма, просто обменивающаяся разделом продуктов по мере увеличения кода.

    class LeadForm(forms.ModelForm):
        product_queryset = []
        products = forms.MultipleChoiceField(choices=product_queryset)

        def __init__(self, *args, **kwargs):
            assigned_users = kwargs.pop('assigned_to', [])
            super(LeadForm, self).__init__(*args, **kwargs)
            self.fields['products'].required = False
            self.fields['products'].choices = [(pro.get('id'),pro.get('name')) for pro in Product.objects.all().values('id','name')]

views.py Я делюсь толькоРаздел продуктов, поскольку код немного длиннее.

    def update_lead(request, pk):
        lead_record = Lead.objects.filter(pk=pk).first()
        template_name = "create_lead.html"
        users = []
        if request.user.role == 'ADMIN' or request.user.is_superuser:
            users = User.objects.filter(is_active=True).order_by('email')
        else:
            users = User.objects.filter(role='ADMIN').order_by('email')
        status = request.GET.get('status', None)
        initial = {}
        if status and status == "converted":
            error = "This field is required."
            lead_record.status = "converted"
            initial.update({
                "status": status, "lead": lead_record.id})
        error = ""
        form=LeadForm(instance=lead_record,initial=initial,assigned_to=users)
        if request.POST:
            form = LeadForm(request.POST, request.FILES,
                            instance=lead_record,
                            initial=initial, assigned_to=users)


            if form.is_valid():               
                if request.POST.getlist('products', []):
                    lead_obj.products.clear()
                    lead_obj.products.add(*request.POST.getlist('products'))
                else:
                    lead_obj.products.clear()                  
                status = request.GET.get('status', None)
                success_url = reverse('leads:list')
                if status:
                    success_url = reverse('accounts:list')
                return JsonResponse({'error': False, 'success_url': success_url})
            return JsonResponse({'error': True, 'errors': form.errors})
        context = {}
        context["lead_obj"] = lead_record
        context["lead_form"] = form
        context["teams"] = Teams.objects.all()
        context['products'] = Product.objects.all()
        context["assignedto_list"] = [
            int(i) for i in request.POST.getlist('assigned_to', []) if i]
        return render(request, template_name, context)

create_lead.html Я использую этот HTML для создания, а также обновления представления. Я просто делюсь продуктами div раздел

                <div class="form-group" style="height:20px;">
                   <label for="exampleInputEmail1">Product{% if products.field %}<span
                        class="error">*</span>{% endif %}</label>
                    <select multiple="multiple">

                      {% for product in products %}
                      <option value="{{product.pk}}" {% if product in products.company_products.all %} selected="" {% endif %}>{{product.name}}</option>
                      {% endfor %}
                  </select>

                  </div>

1 Ответ

0 голосов
/ 15 октября 2019

В шаблоне вы должны избегать визуализации формы "вручную". Вы должны визуализировать форму, используя django систему рендеринга форм .:

<form action="/your-name/" method="post">
    {% csrf_token %}
    {{ lead_form }}
    <input type="submit" value="Submit">
</form>

Если вам нужно визуализировать только это поле вручную :

        <div class="form-group" style="height:20px;">
           <label for="exampleInputEmail1">Product{% if products.field %}<span
                class="error">*</span>{% endif %}</label>

           {{ lead_form.products }}

          </div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...