Django:Updateview inlineformset错误-(隐藏的字段ID)此字段为必填

时间:2018-10-04 12:30:55

标签: django class django-views inline-formset

我正在尝试在Django中编辑具有inlineformset的updateview。以下是model.py,forms.py,views.py和模板文件。表单没有保存,因此我在views.py的post方法中放置了一个print(actor_form)来查看发生了什么。我得到以下html代码:

<input type="hidden" name="actor-TOTAL_FORMS" value="1" id="id_actor-TOTAL_FORMS" /><input type="hidden" name="actor-INITIAL_FORMS" value="1" id="id_actor-INITIAL_FORMS" /><input type="hidden" name="actor-MIN_NUM_FORMS" value="0" id="id_actor-MIN_NUM_FORMS" /><input type="hidden" name="actor-MAX_NUM_FORMS" value="1000" id="id_actor-MAX_NUM_FORMS" />
<tr><td colspan="2"><ul class="errorlist nonfield"><li>(Hidden field id) This field is required.</li></ul></td></tr>
<tr><th><label for="id_actor-0-actorName">ActorName:</label></th><td><input type="text" name="actor-0-actorName" value="www" maxlength="50" id="id_actor-0-actorName" /></td></tr>
<tr><th><label for="id_actor-0-DELETE">Delete:</label></th><td><label for="id_actor-0-DELETE" class="checkbox-inline"><input type="checkbox" name="actor-0-DELETE" id="id_actor-0-DELETE" />
</label><input type="hidden" name="actor-0-id" id="id_actor-0-id" /><input type="hidden" name="actor-0-useCase" id="id_actor-0-useCase" /></td></tr>

在上面的第二行中,您可以看到-((隐藏的字段ID)此字段是必填字段。

我相信这是导致内联无法保存的原因,但是我不知道如何解决它。

这是模型。py

class Profile(models.Model):
    profileName = models.CharField(max_length=50)
    profileBoundary = models.CharField(max_length=3, choices = BOUNDARY_CHOICES)
class Actor(models.Model):
    profile= models.ForeignKey(UseCaseProfile, on_delete=models.CASCADE , null=True, blank=True)
    actorName=models.CharField(max_length=50)

这是表格。py

class ProfileForm(ModelForm):
    class Meta:
        model=Profile
        exclude = ('project',)
class ActorForm(ModelForm):
    class Meta:
        model=Actor
        fields ='__all__'

views.py

class ProUpdateView(UpdateView):
template_name = 'Update.html'
model = Profile
form_class = ProfileForm

def get_success_url(self):
    self.success_url = reverse_lazy ('useCaseExtract:UseCaseList', kwargs={'project': self.kwargs['project']})
    return self.success_url

def get_context_data(self, **kwargs):
    context = super(ProUpdateView, self).get_context_data(**kwargs)
    if self.request.POST:       
        context['actor_form'] = ActorFormSet(self.request.POST, instance=self.object, prefix='actor')            
    else:                              
        context['actor_form'] = ActorFormSet(instance=self.object,prefix='actor')
    return context

def post(self, request, *args, **kwargs):
    self.object = self.get_object()        
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    actor_form = ActorFormSet(self.request.POST,prefix='actor')

    print(actor_form)
    if (form.is_valid() and actor_form.is_valid()):
        return self.form_valid(form, actor_form)
    else:
        return self.form_invalid(form, actor_form)

def form_valid(self, form, actor_form):
    self.object = form.save()
    actor_form.instance = self.object
    actor_form.save()
    return HttpResponseRedirect(self.get_success_url())

def form_invalid(self, form, actor_form):
    return self.render_to_response( self.get_context_data(form=form, actor_form=actor_form))

模板

<form method="POST" enctype="multipart/form-data" style="margin-left: 40px; margin-right: 40px">        
    {% for hidden_field in form.hidden_fields %}      
    {% endfor %}    
    {% csrf_token %}
    {{ form.management_form }}
    {{ form.non_form_errors }}
        <h4>Use Case Profile:</h4>

            {% bootstrap_form  form %}          

    <h4>Supported Role(s):</h4>         
        <div >
        {% for hidden_field in actor_form.hidden_fields %}      
    {% endfor %}
            {{ actor_form.management_form }}
            {{ actor_form.non_form_errors }}
            {% for aform in actor_form %}
                <div >
                    {{ aform.actorName.errors }}

                    {% bootstrap_field aform.actorName %}               
                </div>          
            {% endfor %}            

        </div>

        </div>  
    <hr style = "border: 0; border-top: 1px solid #999; margin: 1em 0;">        
    <a style="margin-left: 350px;" class="text-danger" href="{% url 'useCaseExtract:UseCaseList' view.kwargs.project %}"> {% bootstrap_icon "remove" %} Cancel</a>
    <input class="btn btn-success" type="submit" value="Update" />    
</form> 
</div>
<!-- main_container ends here -->
</div> 

1 个答案:

答案 0 :(得分:1)

如果您要在模板中手动布置表单集,请确保遍历隐藏字段。

{% for hidden in form.hidden_fields %}
    {{ hidden }}
{% endfor %}

参考: https://docs.djangoproject.com/es/2.1/topics/forms/#looping-over-hidden-and-visible-fields