I am trying to initialize a form containing a ChoiceField
in django. I have the following code:
# in file models.py
class Locality(models.Model):
locality = models.CharField(primary_key=True, unique=True, max_length=36)
def __unicode__(self):
return self.locality
# in file forms.py
class RegisterForm(forms.Form):
def __init__(self, *args, **kwargs):
self.username = forms.CharField(required=True)
self.email = forms.EmailField(required=True)
self.locality = forms.ChoiceField(widget=forms.Select())
self.fields['locality'].choices = [l.locality for l in Locality.objects.all()]
but oon the shell, once I try to instanciate:
r = RegisterForm(username="toto", email="a@b.com")
I receive 'RegisterForm' object has no attribute 'fields' error
. Is this happening as the object is not already formed? How can I access to the ChoiceField
?
Any help appreciated.