0

I am beginner in Django and Currently I was developing the code in Django and was not able to figure out the difference between the two.

class MyForm(forms.Form):
    myfield = forms.ChoiceField(choices=[(u.id, u.username) for u in User.objects.filter(type="TYPE1")])

and

class MyForm(forms.Form):
    pass

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['myfield'] = forms.ChoiceField(choices=[(u.id, u.username) for u in User.objects.filter(type="TYPE1")])

even though purpose of both code is same but I am unable to spot the difference between the two as what is going on behind the scene.

Nikhil Gupta
  • 376
  • 3
  • 19
  • It is a proxy object that walks over the MRO and thus looks for the first implementation in the ancestors (according to the MRO). – Willem Van Onsem Jul 18 '18 at 13:11
  • But here it does not matter (well there are some slightly differences, for example you define a class attribute). Therefore the former is advisable over the latter. – Willem Van Onsem Jul 18 '18 at 13:12
  • https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods check here – Nagashayan Jul 18 '18 at 13:15
  • Not wanting to sound mean or an arse but your best bet for this kind of question is too google it ("what is the use of super in python"). Take a good read over https://www.pythonforbeginners.com/super/working-python-super-function, once you have finished reading it; i would recommend leaving it a couple of days then re-read it again. It's a good article. – Matt Seymour Jul 18 '18 at 13:17

0 Answers0