如何从Django中的inlineadmin获取当前模型实例

时间:2013-02-19 05:04:50

标签: python django django-admin

我正在使用django documentation中提供的formfield_for_manytomany。 但是在该函数内部,我需要获取正在编辑的当前父对象。

def formfield_for_manytomany(self, db_field, request, **kwargs):
    if db_field.name == "car":
        kwargs["queryset"] = Cars.objects.filter(owner=person)
    return super(myModel, self).formfield_for_manytomany(db_field, request, **kwargs)

如何让被编辑的人?

3 个答案:

答案 0 :(得分:21)

如果无法轻易从person获取request,您可能需要通过覆盖ModelAdmin.get_form()InlineModelAdmin.get_formset()手动传递它:

from functools import partial

class MyModelAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        kwargs['formfield_callback'] = partial(self.formfield_for_dbfield, request=request, obj=obj)
        return super(MyModelAdmin, self).get_form(request, obj, **kwargs)

    def formfield_for_dbfield(self, db_field, **kwargs):
        person = kwargs.pop('obj', None)
        formfield = super(MyModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        if db_field.name == "car" and person:
            formfield.queryset = Cars.objects.filter(owner=person)
        return formfield 

# or its inline
class MyInlineModelAdmin(admin.StackedInline):
    def get_formset(self, request, obj=None, **kwargs):
        kwargs['formfield_callback'] = partial(self.formfield_for_dbfield, request=request, obj=obj)
        return super(MyInlineModelAdmin, self).get_formset(request, obj, **kwargs)

    def formfield_for_dbfield(self, db_field, **kwargs):
        person = kwargs.pop('obj', None)
        formfield = super(MyInlineModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        if db_field.name == "car" and person:
            formfield.queryset = Cars.objects.filter(owner=person)
        return formfield 

class MyModelAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        kwargs['formfield_callback'] = partial(self.formfield_for_dbfield, request=request, obj=obj)
        return super(MyModelAdmin, self).get_form(request, obj, **kwargs)

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name != "car":
            kwargs.pop('obj', None)
        return super(MyModelAdmin, self).formfield_for_dbfield(db_field, **kwargs)

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        person = kwargs.pop('obj', None)
        if db_field.name == "car" and person:
            kwargs['queryset'] = Cars.objects.filter(owner=person)
        return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

答案 1 :(得分:4)

要访问InlineModelAdmin的父ModelAdmin的模型实例,我过去曾使用过这个hack:

class PersonAdmin(ModelAdmin):
    def get_formsets(self, request, obj=None, *args, **kwargs):
        for inline in self.inline_instances:
            inline._parent_instance = obj
            yield inline.get_formset(request, obj)

class CarInline(TabularInline):
    _parent_instance = None

    def get_formset(self, *args, **kwargs):
        def formfield_callback(field, **kwargs):
            formfield = field.formfield(**kwargs)
            if field.name == 'car':
                formfield.queryset = Cars.objects.filter(owner=self._parent_instance)
            return formfield

        if self._parent_instance is not None:
            kwargs['formfield_callback'] = formfield_callback
        return super(CarInline, self).get_formset(*args,
                                                                 **kwargs)

答案 2 :(得分:3)

我在get_formset() / get_formsets() / formfield_for_dbfield()中发现了一种没有任何黑客攻击的清洁方式。使用Django的Request对象(您有权访问)来检索request.path_info,然后从resolve匹配中的args中检索PK。例如:

from django.contrib import admin
from django.core.urlresolvers import resolve
from app.models import YourParentModel, YourInlineModel


class YourInlineModelInline(admin.StackedInline):
    model = YourInlineModel

    def get_parent_object_from_request(self, request):
        """
        Returns the parent object from the request or None.

        Note that this only works for Inlines, because the `parent_model`
        is not available in the regular admin.ModelAdmin as an attribute.
        """
        resolved = resolve(request.path_info)
        if resolved.args:
            return self.parent_model.objects.get(pk=resolved.args[0])
        return None

    def has_add_permission(self, request):
        parent = self.get_parent_object_from_request(request)
        if parent and parent.is_active is True:
            return False
        return super(YourInlineModelInline, self).has_add_permission(request)

    def get_formset(self, request, *args, **kwargs):
        """
        Using the get_formset example from above to override the QuerySet.
        """
        def formfield_callback(field, **kwargs):
            formfield = field.formfield(**kwargs)
            if field.name == 'car':
                formfield.queryset = self.parent_model.objects.filter(
                    owner=self.get_parent_object_from_request(request)
                )
            return formfield

        if self.get_parent_object_from_request(request) is not None:
            kwargs['formfield_callback'] = formfield_callback

        return super(YourInlineModelInline, self).get_formset(*args, **kwargs)


@admin.register(YourParentModel)
class YourParentModelAdmin(admin.ModelAdmin):
    inlines = [YourInlineModelInline]