注意:Django / Python初学者,希望这个问题很清楚。
我创建了一个表单,可以在一个表单中一次编辑模型(Guest)的多个实例,并同时提交。
此Guest模型链接到父模型Invite,表示多个Guest连接到单个Invite。
我已经设法为每个访客创建了一个formset并显示它,但是当我提交它时,我收到以下错误。
NOT NULL constraint failed: app_guest.invite_id
我假设这是因为它没有邀请保存数据,但是我无法确定将invite_id
放在哪里&# 39; s要求。
到目前为止,我的观点是:
def extra_view(request, code):
# Get the specific invite
invite = get_invite(code)
# Get guests attached to this invite
guests = invite.guest_set.all()
# Get the context from the request.
context = RequestContext(request)
# Store object of guests marked as attending
guests_attending = invite.guest_set.filter(attending=True, invite=invite)
form = ExtraForm(data=request.POST or None)
if form.is_valid():
# Save the new category to the database.
### FAILS HERE
form.save(commit=True)
# Go to Confirm page
HttpResponseRedirect('confirm')
else:
# The supplied form contained errors - just print them to the terminal for now
print form.errors
if guests_attending.count() > 1:
# If the request was not a POST, display the form to enter details.
guest_formset = modelformset_factory(Guest, form=ExtraForm, extra=0, max_num=guests_attending.count())
# Filter the formset so that only guests marked as attending are
filtered_guest_form = guest_formset(queryset=guests.filter(attending=True))
# Return the view
return render_to_response('weddingapp/extra.html', {'GuestForm': filtered_guest_form, 'invite': invite, 'guests_attending': guests_attending, 'form_errors': form.errors}, context)
else:
# Since there's no guests to create a form for, return Confirm view
return render(request, 'weddingapp/confirm.html', {
'invite': invite,
})
这是我的嘉宾模特:
@python_2_unicode_compatible
class Guest(models.Model):
invite = models.ForeignKey(Invite, on_delete=models.CASCADE)
guest_name = models.CharField(max_length=200)
diet = models.CharField(max_length=250)
transport = models.NullBooleanField(default=False, null=True)
attending = models.NullBooleanField(null=True)
def __str__(self):
return self.guest_name
以下是表格:
class ExtraForm(forms.ModelForm):
diet = forms.CharField(max_length=128, help_text="Please enter your diet restrictions", required=False)
transport = forms.BooleanField(initial=False, help_text="Will you be needing transport?", required=False)
# An inline class to provide additional information on the form.
class Meta:
# Provide an association between the ModelForm and a model
model = Guest
fields = ('diet', 'transport')
真的很感激任何帮助。即使是关于更好地构建所有这些的建议也会有所帮助。感谢。
答案 0 :(得分:0)
您的表单不包含#include <stdio.h>
#include <stdlib.h>
void* create_2D_array (size_t x, size_t y)
{
int (*array)[y] = malloc( sizeof(int[x][y]) );
for (size_t i = 0; i < x; ++i)
{
for (size_t j = 0; j < y; ++j)
{
array[i][j] = (int)(i + j);
}
}
return array;
}
void print_2D_array (size_t x, size_t y, int array[x][y])
{
for (size_t i = 0; i < x; ++i)
{
for (size_t j = 0; j < y; ++j)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
}
int main (void)
{
size_t x = 5;
size_t y = 3;
int (*arr_2D)[y];
arr_2D = create_2D_array(x, y);
print_2D_array(x, y, arr_2D);
free(arr_2D);
return 0;
}
字段,因此您必须在保存到数据库之前进行设置。
invite