4👍
Use something like this:
class RandomValueForm(ModelForm):
myfield = models.IntegerField(default=0)
def __init__(self, *args, **kwargs):
super(RandomValueForm, self).__init__(*args, **kwargs)
self.fields['myfield'].initial = my_random_generator()
2👍
You got this error because you would have tried accessing fields
on self
without calling the __init__
of superclass. So, first you need to call superclass __init__
i.e __init__
of ModelForm and then you can access fields
.
class MyModelForm(ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.fields['myfield'].initial = my_random_number()
- [Django]-How to implement countdown timer in django
- [Django]-Django: Add field to model formset
- [Django]-Django – Could i18n.set_language() store in User profile
- [Django]-Django-import-export before_import_row to automatically create object if it does not exist
Source:stackexchange.com