Question

I'm confused on whether I can set initial data to a field with crispy forms. I have one field in which I generate a number and want to set that to a specific field. If I do anything like:

def __init__(self, *args, **kwargs):
    self.fields['medical_record_number'].initial = 'whatever you want'

I get a error stating that name 'self' is not defined.

I am assuming there is something different I have to do while using crispy forms to accomplish the same thing?

Thanks,

Tom

Was it helpful?

Solution

Nothing to do with crispy-forms, this is regular Django forms code. You have forgotten to call parent constructor, if you are using python 2.X do:

def __init__(self, *args, **kwargs):
    super(YourFormClassName, self).__init__(*args, **kwargs)
    self.fields['medical_record_number'].initial = 'whatever you want'

Beware that maybe you prefer to set initial in your Django form field:

whatever = forms.CharField(initial="whatever you want")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top