我正在尝试为提交的表单设置FK,它正在抓住我!哦,亲爱的!
这是我在提交表单时遇到的错误。
/ build-classroom /中的IntegrityError reports_classroom.user_id可能不是NULL
我认为它与ClassroomForm中的exclude = ("user",)
有关,或者可能与我的观看代码classroom.user = request.user
forms.py
class ClassroomForm(forms.ModelForm):
class Meta:
model = Classroom
exclude = ("user",)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.form_action = ''
self.helper.form_tag = False
self.helper.layout = Layout(
Fieldset('Create a New Classroom','name'),
)
super(ClassroomForm, self).__init__(*args, **kwargs)
class StudentForm(forms.ModelForm):
class Meta:
model = Student
exclude = ("classroom",)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.form_action = ''
self.helper.form_tag = False
self.helper.layout = Layout(
Fieldset(
'Add Student',
Div('first_name', css_class="large-5 columns"),
Div('last_name', css_class="large-5 columns"),
Div('birth_date', css_class="large-2 columns"),
Div('gender', css_class="large-2 columns"),
Div('ethnicity', css_class="large-2 columns"),
Div('race', css_class="large-2 columns"),
Div('free_or_reduced_lunch','prior_special_ed_services', css_class="large-6 columns"),
),
)
super(StudentForm, self).__init__(*args, **kwargs)
views.py
@login_required
def build_classroom(request):
# Empty formset forms should be required
class RequiredFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(RequiredFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
StudentFormSet = formset_factory(StudentForm, max_num=100, formset=RequiredFormSet)
if request.method == 'POST': # If the form has been submitted...
classroom_form = ClassroomForm(request.POST) # A form bound to the POST data
# Create a formset from the submitted data
student_formset = StudentFormSet(request.POST, request.FILES)
if classroom_form.is_valid() and student_formset.is_valid():
classroom = classroom_form.save(commit=False)
classroom.user = request.user
for form in student_formset.forms:
student = form.save(commit=False)
student.classroom = classroom
student.save()
return HttpResponseRedirect('/') # Redirect to a 'success' page
else:
classroom_form = ClassroomForm()
student_formset = StudentFormSet()
# For CSRF protection
# See http://docs.djangoproject.com/en/dev/ref/contrib/csrf/
c = {'classroom_form': classroom_form,
'student_formset': student_formset,
}
c.update(csrf(request))
return render_to_response('reports/build_classroom.html', c)
build_classroom.html
<form action="" method="POST">{% csrf_token %}
<div class="section">
{% crispy classroom_form %}
</div>
{{ student_formset.management_form|crispy }}
{% for form in student_formset.forms %}
<div class="item">
{% crispy form %}
<p style=""><a class="delete button secondary" href="#">Delete</a></p>
</div>
{% endfor %}
<a id="add" class='button' href="#">Add another student</a>
<input type="submit" value="Submit" class='button' />
</form>
models.py
class Classroom(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=30)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __unicode__ (self):
return self.name
我遗漏了一些jQuery和其他代码。