Django以内联形式admin获取实例 [英] Django get instance in inline form admin

查看:66
本文介绍了Django以内联形式admin获取实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有内联表单类:

class ItemColorSelectForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ItemColorSelectForm, self).__init__(*args, **kwargs)
        #here i need current object

内联类:

class ItemColorSelectInline(generic.GenericTabularInline):
    model = ColorSelect
    extra = 1
    form = ItemColorSelectForm

Admin类

class ItemAdmin(admin.ModelAdmin):
    inlines = [ItemColorInline,]

问题:如何在 ItemColorSelectForm中获取当前对象

打印垃圾返回:

{'auto_id': u'id_%s', 'prefix': u'catalog-colorselect-content_type-object_id-__prefix__', 'empty_permitted': True}


推荐答案

解决方案:
覆盖内联类中的表单集方法

Solution: Override the formset method in Inline class

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

这篇关于Django以内联形式admin获取实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆