Can a dictionary be passed to django models on create?
For any model DummyModel
you can use DummyModel.objects.create(**data_dict)
. Does not require save after the create.
If title
and body
are fields in your model, then you can deliver the keyword arguments in your dictionary using the ** operator.
Assuming your model is called MyModel
:
# create instance of model
m = MyModel(**data_dict)
# don't forget to save to database!
m.save()
As for your second question, the dictionary has to be the final argument. Again, extra
and extra2
should be fields in the model.
m2 =MyModel(extra='hello', extra2='world', **data_dict)
m2.save()