N
N
nurzhannogerbek2017-04-30 17:48:54
Django
nurzhannogerbek, 2017-04-30 17:48:54

Why is the content of the form changing?

Hello!
I'm trying to create a dynamic list for choices in a symbol field on a form. The list itself is formed in views and then passed to the form constructor. In the form, everything is displayed correctly initially, but when you try to add a new value, the form changes as shown in the figure. What happened?
It is necessary to add data from the Dictionary model to the hoises, but only those that are not in the Requirement. What am I doing wrong?
When loading the content of the form:
5432813944914733a6efb663bc2ed44f.PNG
When trying to add (pressing submit), the content of the form changes:
62e94e2b7a8e4192981917ccc6f7bce4.PNG
models.py:

class Dictionary(models.Model):
    symbol = models.CharField(_('Symbol'), max_length=250)
    name = models.CharField(_('Name'), max_length=250)

class Requirement(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    symbol = models.CharField(_('Symbol'), max_length=250)
    name = models.CharField(_('Name'), max_length=250)

forms.py:
class RequirementForm(forms.ModelForm):
    symbol = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)

    class Meta:
        model = Requirement
        fields = ('symbol',)

    def __init__(self, final_list, *args, **kwargs):
        super(RequirementForm, self).__init__(*args, **kwargs)
        self.fields['symbol'].choices = [(x[0], x[1]) for x in final_list)]

views.py:
def requirement_add(request, project_code):
    data = dict()
    project = get_object_or_404(Project, pk=project_code)

    requirements = Requirement.objects.filter(project=project_code)
    result = []
    for x in requirements:
         v = (x.symbol, x.name)
         result.append(v)
    full_result = [(x.symbol, x.name) for x in Dictionary.objects.all()]
    final_list= list(set(full_result) ^ set(result))

    if request.method == 'POST':
        form = RequirementForm(request.POST, final_list)
        if form.is_valid():
            requirement_dict = dict(((x.symbol, x.name) for x in Dictionary.objects.all()))
            symbols = form.cleaned_data.get('symbol')
            requirement = form.save(commit=False)
            for symbol in symbols:
                requirement.project = project
                requirement.symbol = symbol
                requirement.name = requirement_dict [symbol]
                requirement.pk = None
                requirement.save()
            data['form_is_valid'] = True
            requirements = Requirement.objects.filter(project=project_code)
            context = {'project': project, 'requirement': requirement, 'requirements': requirements}
            data['html_requirement'] = render_to_string('project/requirement_list.html', context)
        else:
            data['form_is_valid'] = False
    else:
        form = RequirementForm(final_list)
    context = {'project': project, 'form': form}
    data['html_requirement_form'] = render_to_string('project/requirement_add.html', context, request=request)
    return JsonResponse(data)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question