set initial value for field when a model insert page loaded in django admin

438 views
Skip to first unread message

sjsade...@gmail.com

unread,
Feb 18, 2017, 1:35:54 AM2/18/17
to Django users
hi, i want set initial value for field when a model insert page loaded in django admin.
i think i can do this work with get_field_queryset method but in django there is no toturial for get_field_queryset.
how to do this work?
thanks ...

Derek

unread,
Feb 18, 2017, 9:07:20 AM2/18/17
to Django users
I have cut and trimmed this from of some of my own code, so may not be 100%:

class MyModelForm(ModelForm):

    class Meta:
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyModelForm, self).__init__(*args, **kwargs)
        self.fields['my_field'].initial= 'some_value'


class MyModelAdmin(ModelAdmin):
    form = MyModelForm
Reply all
Reply to author
Forward
0 new messages