質問

I have inline:

class GenderInline(admin.StackedInline):
    model = models.GenderModel1
    extra = 0

I want to use this inline in different admin forms.

Once I want that his model will be models.GenderModel1, and once models.GenderModel2 (according to the AdminForm this inline is related)

Can I do that? Or the only way is to duplicate the inline? (which I prefer not... I need to duplicate 10 inlines.. ):

class GenderInline1(admin.StackedInline):
    model = models.GenderModel1
    extra = 0

class GenderInline2(admin.StackedInline):
    model = models.GenderModel2
    extra = 0
役に立ちましたか?

解決

Actually you may create classes on the fly using type to avoid defining multipl classes. Define a function that crates an inline like this:

def get_inline_by_model(m):
    return type(
        'DynamicInline', 
        (admin.StackedInline, ), 
        {'model':m, 'extra':0} 
    )

And then in your Admin class you can just define your inline like:

inlines = ( get_inline_by_model (models.GenderModel1 ) ,  ) 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top