In my django admin.py I have a custom widget based on a select menu.
I want it to behave differently (show differents items) when it’s showed in creation mode (add an object) or update mode, because this fields allows the user to choose video from an hosted playlist.
I can pass a different item list (“choices” argument) when calling the widget in the modelForm, but how can I get this information : add or update mode ?
UPDATE : thanks to second here is the answer
class VideoAdminForm(forms.ModelForm):
class Meta:
model = Video
widgets = {'id_vimeo' : VimeoSelectWidget()}
def __init__(self, *args, **kwargs):
super(VideoAdminForm, self).__init__(*args, **kwargs)
if self.instance.pk is None:
self.fields['id_vimeo'] = forms.ChoiceField(choices=VimeoList())
else:
self.fields['id_vimeo'] = forms.ChoiceField(choices = Video.objects.all().values_list('id_vimeo','titre'))
have a look at the model instance associated with the form. if it has a
pk, it exists and is being changed, ifpkisNone, it’s being created