为什么模型表单集不保存在Django中

时间:2020-10-17 07:31:33

标签: python django django-views django-templates django-2.0

我有一个表单集,我正在尝试将其保存回去。但是,当我尝试检查employee_academic_forms.is_valid()并保存时,即使表单集中的任何内容都没有更改,验证也总是会失败。我正在使用formset显示现有数据并尝试对其进行编辑。我不知道我要去哪里错了。有人可以帮忙吗?

视图:

def employedit(request, pk, id):
    employ_academic = EmployAcademicInfo.objects.filter(employ_id=pk)
    employ_academic_forms = EmployAcademicUpdateFormSet(queryset=employ_academic)
    if request.method == 'POST':
        employ_academic_forms = EmployAcademicUpdateFormSet(request.POST, queryset=employ_academic)
        if employ_academic_forms.is_valid():
            user_obj = User.objects.get(id=pk)
            name = employ_basic_forms.cleaned_data['name']
            email = employ_basic_forms.cleaned_data['email']


            user_obj.username=email
            user_obj.first_name=name
            user_obj.email=email
            user_obj.save()


            instances = employ_academic_forms.save(commit=False)
            print(instances)
            for instance in instances:
                instance.employ_id = user_obj
                instance.save()
            return redirect('employ-list')
    context = {
        'employ_academic_forms':employ_academic_forms,

    }
    return render(request, 'admins/employ/edit_employ.html', context)

表格:

EmployAcademicUpdateFormSet = modelformset_factory(
    EmployAcademicInfo,
    exclude = ['employ_id'],
    extra=0,
    labels = {
        'degree': 'Enter Employ Degree',
        'last_passing_institution_name': 'Enter Employ Passing Institution',
        'last_passing_year': 'Enter Employ Passing Year',
        
    },
    widgets = {
        'degree' : forms.Select(attrs={'class':'form-control form-control-lg', 'placeholder':'Enter degree'}),
        'last_passing_institution_name' : forms.TextInput(attrs={'class':'form-control form-control-lg', 'placeholder':'Enter institution name'}),
        'last_passing_year' : forms.DateInput(attrs={'class':'form-control form-control-lg', 'type':'date'}),
        
    },   
    )

HTML:

{% extends 'base/base.html' %} 
{% load static %}
{% load crispy_forms_tags %} 
{% block content %}
<div class="card">
    <form class="form-horizontal" action="" method="post">
        {% csrf_token %}
        <div class="card-body">
            <div class="card-body">
                <div class="form-horizontal">
                        {{ employAcademicFormSet.management_form }}
                        {% for form in employ_academic_forms %} 
                        {% for field in form.visible_fields %}
                            <div class="form-group row">
                                <label class="col-md-3 col-form-label" for="text-input"><h6>{{ field.label_tag }}</h6></label>
                                <div class="col-md-9">{{ field }}</div>
                            </div>
                        {% endfor %} 
                        {% endfor %}
                    </div>
            </div>
        </div>
        <div class="card-footer">
            <button class="btn btn-lg btn-primary" type="submit">Submit</button>
        </div>
    </form>
</div>
{% endblock %}

0 个答案:

没有答案