i’m new to django, i’m trying to make a class-based views where i can choose what model or form i want to use (or not using form), for example :
views.py :
class Foo(object):
#some code here
def get_query(self):
if self.form.is_valid():
return self.form.cleaned_data['field_name']
return ''
lets say i got 2 forms :
form.py :
class Bar(forms.Form):
title = forms.CharField(max_length = 200)
content = forms.CharField(max_length = 200)
forms.py :
class Baz(Bar):
date = DateField()
from the views above i got get_query method where i take the value from a field name, but the problem is not all forms are same, Bar got 2 fields and Baz got 3 fields, maybe i want 100 fields(just maybe), im thinking of using loop, so is there any way to get all the value from the field? is it possible?
What you are looking for is self.fields which is I think a dict(?) and contains all the Form fields and names and so on. For any detail how dynamically to extract fields of unknown big Form just take a look at the code in BaseForm._clean_fields to get general idea of how to access them.