Django-admin内联默认值

时间:2017-10-03 07:11:32

标签: django django-models django-forms django-views django-admin

admin.py

class CaseNoteInline(admin.TabularInline):
    model = CaseNote
    form = CaseNoteInlineForm
    extra = 1
    max_num = 5
    readonly_fields = ['User']


    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'User':
            kwargs['initial'] = request.user.id
            return db_field.formfield(**kwargs)
        return super(CaseNoteInline, self).formfield_for_foreignkey(
            db_field, request, **kwargs
        )

我在CaseNote中有一些专栏是" User"我想为此设置默认值。 just i want to initiate this selectbox

0 个答案:

没有答案