user1778743 user1778743 - 1 month ago 10
Python Question

overriding modelform choices with __init__

I've been trying to get the fields for a choice to change based on what i give it in my view and i get

name 'self' is not defined


here is my view.py

def CarOwnerSearch(request):
if request.user.is_authenticated():
form = carOwnerForm.__init__(self,'B','None','C')
return render (request,'carmanager/CarOwnerSearch.html', {'form': form})


OK ive got a modelform

class carOwnerForm(forms.ModelForm):
first_name = forms.charfield()
last_name = forms.charfield()

def __init__(self, a,b,c):
super(carOwnerForm,self).__init(a,b,c)
self.field["ownership_Type"].choice = [a,b,c]

class Meta:
model = CarOwner
fields = ['ownership_type','CarModel']


and this is my model.py

class CarOwner(models.Model):
ownershipTpes_choices = ( ('R','Rented'),('L','Leased'),('O','Owned'))
ownership_Type = models.CharField(max_length=1,
choices=ownershipTypeChoices)

Answer

This is how you would be initializing the form:

form = carOwnerForm.__init__(self,'B','None','C')

should be

form = carOwnerForm('B', None, 'C')