我一直在使用一个名为ModelChoiceField的表单字段并查询所有对象,但这并不是我打算用它来实现的.
class PictureForm(forms.ModelForm):
Whiteboard = forms.ModelChoiceField(queryset=Whiteboard.objects.all())
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用ModelChoiceField来查询属于特定用户的所有WhiteBoard对象
Whiteboard = forms.ModelChoiceField(queryset=Whiteboard.objects.filter(user=request.user))
Run Code Online (Sandbox Code Playgroud)
但我发现请求没有传递给ModelForm.我一直在搜索SO以寻求各种解决方案,一个解决方案是覆盖表单的init()
这可能是与我的问题相关的最接近的问题 如何在Django中的ModelForm中使用该请求
这是他的解决方案.如果他的解决方案是覆盖视图中的查询集.他如何在forms.py中创建没有查询集的ModelChoiceField
就我而言,我希望用户过滤掉所有白板.我怎么能这样做?
我的模块的一部分
class Whiteboard(models.Model):
Category =models.CharField(max_length=30,choices=CATEGORY)
user = models.ForeignKey(User)
name = models.CharField(max_length=100)
picture = models.OneToOneField('Picture',related_name='picture',blank=True,null=True)
def __unicode__(self):
return self.name
class Picture(models.Model):
user = models.ForeignKey(User)
Whiteboard = models.ForeignKey(Whiteboard,blank=False,null=False,related_name='board')
image = models.FileField(upload_to="images/",blank=True)
description = models.TextField()
is_primary = models.BooleanField(default=False)
def __unicode__(self):
return self.description
Run Code Online (Sandbox Code Playgroud)
我的views.py
def PictureCreator(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
if request.method == "POST":
form = PictureForm(request.POST , request.FILES)
if form.is_valid():
picture = Picture(user=request.user)
image = request.FILES.get('image')
if image:
picture.image = form.cleaned_data['image']
description = form.cleaned_data['description']
if description:
picture.description = form.cleaned_data['description']
if board:
picture.board = form.cleaned_data['board']
picture.save()
board = Whiteboard.objects.get(Whiteboard=picture.board)
the_id = board.id
return HttpResponseRedirect(reverse('world:Boat', kwargs={'animal_id': the_id }))
return render(request,'picture.html',{'form':PictureForm()})
Run Code Online (Sandbox Code Playgroud)
picture.html
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type = "submit" value= "Add Picture" />
</form>
Run Code Online (Sandbox Code Playgroud)
在类定义期间根本不要设置过滤器.您将在视图中覆盖它.
form = PictureForm()
form.fields['whiteboard'].queryset = Whiteboard.objects.filter(user=request.user)
class PictureForm(ModelForm):
whiteboard = forms.ModelChoiceField(queryset=Whiteboard.objects.none(),)
Run Code Online (Sandbox Code Playgroud)
传user中init然后在视图中的表格,您必须通过用户价值PictureForm(request.user)
class PictureForm(forms.ModelForm):
class Meta:
model = Picture
def __init__(self, user, *args, **kwargs):
super(PictureForm, self).__init__(*args, **kwargs)
self.fields['Whiteboard'].queryset = Whiteboard.objects.filter(user=user)
def PictureCreator(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
if request.method == "POST":
form = PictureForm(request.user, request.POST, request.FILES)
if form.is_valid():
picture = Picture(user=request.user)
image = request.FILES.get('image')
if image:
picture.image = form.cleaned_data['image']
description = form.cleaned_data['description']
if description:
picture.description = form.cleaned_data['description']
if board:
picture.board = form.cleaned_data['board']
picture.save()
board = Whiteboard.objects.get(Whiteboard=picture.board)
the_id = board.id
return HttpResponseRedirect(reverse('world:Boat', kwargs={'animal_id': the_id }))
return render(request,'picture.html',{'form':PictureForm(request.user)})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9536 次 |
| 最近记录: |