I'm sorry if this is a stupid question, but I'm having trouble finding out how to create a new instance of a model inside the views.
Referencing this question, I tried doing
foo = FooModel()
save()
but I got a NameError: name 'save' is not defined. I then tried
bar = BarModel.objects.create()
but got AttributeError: 'Manager' object has no attribute 'Create'.
Am I not understanding something very trivial? Maybe those commands are just for the command line? In that case, how do I create new objects, or filter them, etc... from code?
For the first example, you need to call the save method on the object, e.g. foo.save()
instead of just save()
:
foo = FooModel()
foo.save()
Your second example looks ok. Make sure you are calling create()
(all lowercase):
bar = BarModel.objects.create()
The message ... no attribute 'Create'.
suggests you are calling BarModel.objects.Create()
, which is incorrect.
If that still doesn't work, then update your question with the actual code and full traceback. Using made up names like FooModel
makes it harder to see the problem.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments