在Django Admin中将请求传递给内联表单?

时间:2016-08-20 04:48:25

标签: forms admin

我正在尝试从Django Admin中的请求中获取用户。我需要的是以内联表单的clean()方法访问请求的用户。我已经完成了一个与下面描述的程序类似的程序,正常ModelForm(即不是内联程序),我成功了。但是,内联我有很多问题。 我有:

class SaleFormset(BaseInlineFormSet):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request')
        super(SaleFormset, self).__init__(*args, **kwargs)

    def _construct_form(self, i, **kwargs):
        kwargs['request'] = self.request
        super(SaleFormset, self)._construct_form(i, **kwargs)


class SaleProductItemInlineForm(ModelForm):
    """
    Custom form for the Sale Product Item Inline used by the
    Sale Admin form.
    """

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request')
        super(SaleProductItemInlineForm, self).__init__(*args, **kwargs)

    class Meta:
        model = SaleProductItem
        fields = "__all__"  

在admin.py中,我有:

class SaleProductItemInline(admin.TabularInline):
    """
    Tabular inline for a SaleProductItem used in the Sale Admin.
    """
    model = models.SaleProductItem
    form = SaleProductItemInlineForm
    formset = SaleFormset

    def get_formset(self, request, obj=None, **kwargs):
        formset_class = super(SaleProductItemInline, self).get_formset(request, obj, **kwargs)

        class Subset(formset_class):
            def __new__(cls, *args, **kwargs):
                kwargs['request'] = request
                return formset_class(*args, **kwargs)

        return Subset

但是,由于此部分,我收到错误'NoneType' object has no attribute 'media'

@property
def media(self):
    # All the forms on a FormSet are the same, so you only need to
    # interrogate the first form for media.
    if self.forms:
        return self.forms[0].media

2 个答案:

答案 0 :(得分:3)

类似于此的方法对我有用:

def SaleProductItemInlineFormFactory(request):
    class SaleProductItemInlineForm(ModelForm):
        def __init__(self, *args, **kwargs):
            # here finally you can do something with your request
            super(SaleProductItemInlineForm, self).__init__(*args, **kwargs)

        class Meta:
            model = SaleProductItem
            fields = "__all__"
    return SaleProductItemInlineForm

def SaleProductItemInlineFactory(request):
    class SaleProductItemInline(admin.TabularInline):
        model = models.SaleProductItem
        form = SaleProductItemInlineFormFactory(request)

    return SaleProductItemInline

class SaleAdmin(admin.ModelAdmin):
    inlines = ()

    # such way we send request to main form
    def get_form(self, request, obj=None, **kwargs):
        form = super(SaleAdmin, self).get_form(request, obj=obj, **kwargs)
        form.request = request
        return form

    # we define inlines with factory to create Inline class with request inside
    def change_view(self, request, object_id, form_url='', extra_context=None):
        self.inlines = (SaleProductItemInlineFactory(request), )
        return super(SaleAdmin, self).change_view(request, object_id)

    # we define inlines with factory to create Inline class with request inside
    def add_view(self, request, form_url='', extra_context=None):
        self.inlines = (SaleProductItemInlineFactory(request), )
        return super(SaleAdmin, self).add_view(request)

答案 1 :(得分:0)

您解决方案的捷径是:

class SaleFormset(BaseInlineFormSet):

    def _construct_form(self, i, **kwargs):
        form = super(SaleFormset, self)._construct_form(i, **kwargs)
        form.request = self.request
        return form

class SaleProductItemInline(admin.TabularInline):
    model = models.SaleProductItem
    form = SaleProductItemInlineForm
    formset = SaleFormset

    def get_formset(self, request, obj=None, **kwargs):
        formset = super(SaleProductItemInline, self).get_formset(request, obj, **kwargs)
        formset.request = request
        return formset