[英]Django admin: how do I filter a ForeignKeyField widget based on the object's (not request.user's) data?
I have Chart
and Module models (see code below). 我有Chart
和模块模型(请参见下面的代码)。 Each chart belongs to a module (via a ForeignKey
). 每个图表都属于一个模块(通过ForeignKey
)。 A chart may have another chart as its parent
(another ForeignKey
). 一个图表可能有另一个图表作为parent
图表(另一个ForeignKey
)。 What I would like in the admin is that the dropdown for parent
on a particular chart only includes charts in the same module . 我在管理员中想要的是,特定图表上的parent
下拉列表仅包含同一模块中的图表 。
I'm looking for a Python solution, not AJAX. 我在寻找Python解决方案,而不是AJAX。 That means that on creating a new chart the dropdown will have to be empty (without a model instance there's no module selected) and that changing the module in the admin won't update the parent
options to match until the model is saved. 这意味着在创建新图表时,下拉列表必须为空(没有模型实例,没有选择模块),并且在保存模型之前,在管理员中更改模块不会更新匹配的parent
选项。 I'm ok with that. 我可以。
There are plenty of similar-sounding questions (and answers) that turn out to filter the options according to the user ; 有很多类似的问题(和答案),可以根据用户过滤选项; ModelAdmin.formfield_for_foreignkey
gets the request as an argument so you can use request.user
, but it doesn't get given a model instance to play with. ModelAdmin.formfield_for_foreignkey
将请求作为参数获取,因此您可以使用request.user
,但不会给定要使用的模型实例。 (I'm using Django 1.3.) (我正在使用Django 1.3。)
My models (highly simplified of course): 我的模型(当然是高度简化的):
class Module(models.Model):
pass
class Chart(models.Model):
module = models.ForeignKey(Module)
parent = models.ForeignKey(Chart, blank=True, null=True)
class ChartAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
# I would like to do this, but have no "instance":
kwargs['queryset'] = Chart.objects.filter(module=instance.module)
return super(ChartAdmin, self).formfield_for_foreignkey(self, db_field, request, **kwargs)
admin.site.register(Chart, ChartAdmin)
Just override the ModelForm
being used: 只需覆盖正在使用的ModelForm
:
class ChartAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ChartAdminForm, self).__init__(*args, **kwargs)
if self.instance.module_id:
self.fields['parent'].queryset = self.fields['parent'].queryset.filter(module=self.instance.module)
class ChartAdmin(admin.ModelAdmin):
form = ChartAdminForm
...
From what i've seen in the source code, kwargs should contain only the widget (not helping !). 根据我在源代码中看到的内容,kwargs应该仅包含小部件(无济于事!)。
One possible hack, would be to override the get_form() modeladmin method, just to set request.current_object. 一种可能的破解方法是重写get_form()modeladmin方法,仅设置request.current_object。 Then, you could use request.current_object in formfield_callback: 然后,您可以在formfield_callback中使用request.current_object:
def get_form(self, request, obj=None, **kwargs):
request.current_object = obj
return super(ChartAdmin, self).get_form(request, obj, **kwargs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
instance = request.current_object
kwargs['queryset'] = Chart.objects.filter(module=instance.module)
return super(ChartAdmin, self).formfield_for_foreignkey(self, db_field, request, **kwargs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.