common.helpers package¶
Module contents¶
-
common.helpers.
model_form_data
(model, form_class, form_kwargs={})[source]¶ Returns a dict that can be used to instantiate a form object. It fills in the model’s data, but filters out fields that are not on the form. Example:
- class Car(models.Model):
brand = CharField(max_length = 50) fuel_tank_size = FloatField() # more fields
- class CreateCarForm(forms.ModelForm):
fields = [‘brand’]
my_car = Car(brand=’Nissan’, fuel_tank_size=60)
model_form_data(my_car, CreateCarForm) # returns {‘brand’: ‘Nissan’}
Note that the returned dict does not have a field ‘fuel_tank_size’, because it is not on the form.