如何在Django ModelForm中过滤ForeignKey选项? [英] How do I filter ForeignKey choices in a Django ModelForm?
问题描述
models.py
中有以下内容: class公司(models.Model):
name = ...
class Rate(models.Model):
company = models.ForeignKey(Company)
name = ...
class Client(models.Model):
name = ...
company = models.ForeignKey(Company)
base_rate = models。 ForeignKey(Rate)
有多个公司
,每个都有一个范围价格
和客户
。每个客户端
应该有一个基础价格
,从它的父母公司的价格
,而不是另一个公司的价格
。
创建一个表单以添加客户端
,我想删除公司
选项(因为已经通过<$ c上的添加客户端按钮选择了$ c>公司页面),并将价格
选项限制在公司
中。
如何在Django 1.0中进行此操作?
我目前的 forms.py
文件目前只是样板:
$来自django.forms导入的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$
model = Client
而 views.py
也是基本的:
from django.shortcuts import render_to_response,get_object_or_404
from models import *
从表单导入*
def addclient(request,company_id):
the_company = get_object_or_404(Company,id = company_id)
如果request.POST:
form = ClientForm(request.POST)
如果form.is_valid():
form.save()
返回HttpResponseRedirect(the_company.get_clients_url())
其他:
form = ClientForm()
返回render_to_response('addclient.html',{'form':form,'the_company':the_company})
在Django 0.96中,我可以在渲染模板之前通过执行以下操作来骇客:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ c>
ForeignKey.limit_choices_to
似乎有前途,但我不知道如何传入 the_company.id
,我不清楚这是否会在管理界面以外工作。
谢谢。 (这似乎是一个非常基本的请求,但如果我应该重新设计一些我可以建议的内容)。
ForeignKey被表示通过django.forms.ModelChoiceField,它是一个ChoiceField,其选项是ModelSet模型。请参阅 ModelChoiceField 的参考。
所以,提供一个QuerySet到该字段的 queryset
属性。取决于您的表单的构建方式。如果你建立一个明确的表单,你将有直接命名的字段。
form.rate.queryset = Rate.objects.filter(company_id = the_company.id)
如果您使用默认的ModelForm对象, form.fields [rate]。queryset = ...
这是在视图中显式完成的。没有黑客。
Say I have the following in my models.py
:
class Company(models.Model):
name = ...
class Rate(models.Model):
company = models.ForeignKey(Company)
name = ...
class Client(models.Model):
name = ...
company = models.ForeignKey(Company)
base_rate = models.ForeignKey(Rate)
I.e. there are multiple Companies
, each having a range of Rates
and Clients
. Each Client
should have a base Rate
that is chosen from it's parent Company's Rates
, not another Company's Rates
.
When creating a form for adding a Client
, I would like to remove the Company
choices (as that has already been selected via an "Add Client" button on the Company
page) and limit the Rate
choices to that Company
as well.
How do I go about this in Django 1.0?
My current forms.py
file is just boilerplate at the moment:
from models import *
from django.forms import ModelForm
class ClientForm(ModelForm):
class Meta:
model = Client
And the views.py
is also basic:
from django.shortcuts import render_to_response, get_object_or_404
from models import *
from forms import *
def addclient(request, company_id):
the_company = get_object_or_404(Company, id=company_id)
if request.POST:
form = ClientForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(the_company.get_clients_url())
else:
form = ClientForm()
return render_to_response('addclient.html', {'form': form, 'the_company':the_company})
In Django 0.96 I was able to hack this in by doing something like the following before rendering the template:
manipulator.fields[0].choices = [(r.id,r.name) for r in Rate.objects.filter(company_id=the_company.id)]
ForeignKey.limit_choices_to
seems promising but I don't know how to pass in the_company.id
and I'm not clear if that will work outside the Admin interface anyway.
Thanks. (This seems like a pretty basic request but if I should redesign something I'm open to suggestions.)
ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet. See the reference for ModelChoiceField.
So, provide a QuerySet to the field's queryset
attribute. Depends on how your form is built. If you build an explicit form, you'll have fields named directly.
form.rate.queryset = Rate.objects.filter(company_id=the_company.id)
If you take the default ModelForm object, form.fields["rate"].queryset = ...
This is done explicitly in the view. No hacking around.
这篇关于如何在Django ModelForm中过滤ForeignKey选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!