简体   繁体   English

Django管理员:如何根据对象(而非request.user)的数据过滤ForeignKeyField小部件?

[英]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.

相关问题 如何基于request.user覆盖django管理表单Foreignkey - how to override django admin form Foreignkey based on request.user Django Rest Api过滤器字段基于request.user的选择选项 - Django Rest Api filter field's select options based on request.user 如何在中间件中修改django的request.user? - How to modify django's request.user in a Middleware? Django-filter:具有基于 request.user 的查询集的 ModelChoiceFilter - Django-filter: ModelChoiceFilter with request.user based queryset 在Django的选择小部件中可以选择request.user - request.user as a choice in select widget in Django 如何通过request.user过滤django-tastypie的ToManyField? - How to filter ToManyField of django-tastypie by request.user? Django:如何使用ModelFormSet通过request.user过滤ForeignKey选择 - Django: How to filter ForeignKey choices by request.user with ModelFormSet 如何写实例用户的权限是request.user? - How to write the permission of a instance's user is the request.user? Django request.user是模型,适用于管理员和普通用户 - Django request.user is model, for admin and normal user Django当我使用基于类的视图时,如何在表单中传递request.user(当前登录的用户)? - Django How I can pass request.user(current logged user) in forms when I use class based view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM