[英]Django form with m2m relationship not saving
I have a form where I want request.user
to populate as little as possible and rely on the views to populate other fields automatically.我有一个表单,我希望request.user
尽可能少地填充并依赖视图自动填充其他字段。
As a result, some of these fields are not rendered on the form.因此,其中一些字段不会呈现在表单上。
The code in my view seems to work fine for the FK relationship
, but some reason the m2m
is failing.在我看来,代码似乎适用于FK relationship
,但由于某些原因, m2m
失败了。
It's probably the first time I am trying to save a form with m2m and I am probably missing something.这可能是我第一次尝试使用 m2m 保存表单,我可能遗漏了一些东西。
At the moment the error I get with the current code is 'VoucherForm' object has no attribute 'user'
.目前,我在当前代码中遇到的错误是'VoucherForm' object has no attribute 'user'
。
If I remove voucherform.user.add(userprofile)
from the views the form will save, but will not add the user.如果我从表单将保存的视图中删除voucherform.user.add(userprofile)
,但不会添加用户。
model model
class UserProfile(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
class Voucher(models.Model):
user = models.ManyToManyField(User, blank=True)
venue = models.ForeignKey(Venue, blank=True, null=True, related_name="vouchervenues", on_delete=models.CASCADE)
title = models.TextField('voucher title', blank=True)
terms = models.TextField('terms & conditions', blank=True)
form形式
class VoucherForm(ModelForm):
class Meta:
model = Voucher
fields = ('title','terms')
labels ={
'title': '',
'terms': '',
}
widgets = {
'title': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Enter title'}),
'terms': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Enter terms'}),
}
views观点
def add_voucher(request, userprofile_id):
url = request.META.get('HTTP_REFERER')
venue = UserProfile.objects.filter(user=request.user).values('venue')
userprofile = UserProfile.objects.get(id=userprofile_id)
submitted = False
if request.method =="POST":
voucherform = VoucherForm(request.POST)
if voucherform.is_valid():
data = voucherform.save(commit=False)
data.user_id = userprofile.id
data.venue_id = venue
data.save()
voucherform.save_m2m()
voucherform.user.add(userprofile)
return HttpResponseRedirect(url)
else:
voucherform = VoucherForm
if 'submitted' in request.GET:
submitted=True
return redirect('venue-loyalty-card',{'submitted':submitted,'userprofile':userprofile})
Basically, the problem is that you haven't mentioned user
field in VoucherForm
at fields
so it says 'VoucherForm' object has no attribute 'user' , you can do the following:基本上,问题是您没有在VoucherForm
的fields
中提到user
字段,所以它说'VoucherForm' object has no attribute 'user' ,您可以执行以下操作:
from django.shortcuts import get_object_or_404
def add_voucher(request, userprofile_id):
url = request.META.get('HTTP_REFERER')
venue = UserProfile.objects.filter(user=request.user).values('venue')
userprofile = UserProfile.objects.get(id=userprofile_id)
submitted = False
if request.method =="POST":
voucherform = VoucherForm(request.POST)
if voucherform.is_valid():
data = voucherform.save(commit=False)
data.user_id = userprofile.id
data.venue_id = venue
data.save()
voucherform.save_m2m()
current_voucher_instance= get_object_or_404(Voucher,id=data.id)
current_voucher_instance.user.add(userprofile.id)
return HttpResponseRedirect(url)
else:
voucherform = VoucherForm
if 'submitted' in request.GET:
submitted=True
return redirect('venue-loyalty-card',{'submitted':submitted,'userprofile':userprofile})
Note: It is better to use
get_object_or_404()
thanget()
as it callsget()
on a given model manager, but it raisesHttp404
instead of the model'sDoesNotExist
exception.注意:使用get_object_or_404()
比get()
更好,因为它在给定的 model 管理器上调用get()
,但它会引发Http404
而不是模型的DoesNotExist
异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.