How do I create an instance of a model in views.py?

Eärendil Baggins

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?

Alasdair

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I print a list from views.py to the console?

From Dev

How do I create an applicative instance for ziplist?

From Dev

How do I create a Storable instance for this type?

From Dev

How do i create an instance of UserManager

From Dev

How do I create an applicative instance for ziplist?

From Dev

How do I create an instance of a generic type?

From Dev

How do I create an instance of the NetworkInterfacesOperations?

From Dev

How to get only one instance of a model in views

From Dev

How do I create a model of a function?

From Dev

How can I create an instance of one model as soon as an instance of a different model is created?

From Dev

How do I create multiple screens/views using phoneGap?

From Dev

How do I check if an argument is an instance of model or the class in Rails?

From Dev

How do I subscribe to a model instance in Sails.JS?

From Dev

How do I generate standardized model instance template code in django?

From Dev

How do I Bind an Instance of a Model to a Parameter in a Controller Action

From Dev

How do I set an instance variable on an Eloquent model?

From Dev

How do I access a global model instance in laravel 4?

From Dev

How do I Bind an Instance of a Model to a Parameter in a Controller Action

From Dev

How do I create a model that "belongsTo" a "hasMany through" join model?

From Dev

How to create shared Model for 2 different Views?

From Dev

How do I create a custom cycling status in discord.py?

From Dev

How do I redirect a user to some other views.py if nothing is found in the query set?

From Dev

How do I create a new instance of an existing class?

From Dev

How do I create new instance of Generic Type Class

From Dev

How do I create a basic class and instance of that class in Forth?

From Dev

How do I create an arbitrary instance for a recursive datatype?

From Dev

How do I create a ListIsomorphic instance for generic vectors?

From Dev

How do I create a ListIsomorphic instance for Vector.Unboxed?

From Dev

Elastic Beanstalk CLI, how do I create the environment with an RDS instance?

Related Related

  1. 1

    How do I print a list from views.py to the console?

  2. 2

    How do I create an applicative instance for ziplist?

  3. 3

    How do I create a Storable instance for this type?

  4. 4

    How do i create an instance of UserManager

  5. 5

    How do I create an applicative instance for ziplist?

  6. 6

    How do I create an instance of a generic type?

  7. 7

    How do I create an instance of the NetworkInterfacesOperations?

  8. 8

    How to get only one instance of a model in views

  9. 9

    How do I create a model of a function?

  10. 10

    How can I create an instance of one model as soon as an instance of a different model is created?

  11. 11

    How do I create multiple screens/views using phoneGap?

  12. 12

    How do I check if an argument is an instance of model or the class in Rails?

  13. 13

    How do I subscribe to a model instance in Sails.JS?

  14. 14

    How do I generate standardized model instance template code in django?

  15. 15

    How do I Bind an Instance of a Model to a Parameter in a Controller Action

  16. 16

    How do I set an instance variable on an Eloquent model?

  17. 17

    How do I access a global model instance in laravel 4?

  18. 18

    How do I Bind an Instance of a Model to a Parameter in a Controller Action

  19. 19

    How do I create a model that "belongsTo" a "hasMany through" join model?

  20. 20

    How to create shared Model for 2 different Views?

  21. 21

    How do I create a custom cycling status in discord.py?

  22. 22

    How do I redirect a user to some other views.py if nothing is found in the query set?

  23. 23

    How do I create a new instance of an existing class?

  24. 24

    How do I create new instance of Generic Type Class

  25. 25

    How do I create a basic class and instance of that class in Forth?

  26. 26

    How do I create an arbitrary instance for a recursive datatype?

  27. 27

    How do I create a ListIsomorphic instance for generic vectors?

  28. 28

    How do I create a ListIsomorphic instance for Vector.Unboxed?

  29. 29

    Elastic Beanstalk CLI, how do I create the environment with an RDS instance?

HotTag

Archive