Skip to main content
fix obsolete link; change punctuation
Source Link
Rob Bednark
  • 28.8k
  • 28
  • 90
  • 131

The documentation says it is the sameThe Django documentation says it is the same. It is just more convenient to make it on one line. You could make a save() on one line too, but it would be more verbose and less readable: -- it is clear you are creating a new object with the create() method.

create(**kwargs)

A convenience method for creating an object and saving it all in one step. Thus:

p = Person.objects.create(first_name="Bruce", last_name="Springsteen") 

and:

p = Person(first_name="Bruce", last_name="Springsteen") p.save(force_insert=True) 

are equivalent.

The force_insert parameter is documented elsewhere, but all it means is that a new object will always be created. Normally you won’t need to worry about this. However, if your model contains a manual primary key value that you set and if that value already exists in the database, a call to create() will fail with an IntegrityError since primary keys must be unique. Be prepared to handle the exception if you are using manual primary keys.

The documentation says it is the same. It is just more convenient to make it on one line. You could make a save() on one line too, but it would be more verbose and less readable: it is clear you are creating a new object with the create() method.

create(**kwargs)

A convenience method for creating an object and saving it all in one step. Thus:

p = Person.objects.create(first_name="Bruce", last_name="Springsteen") 

and:

p = Person(first_name="Bruce", last_name="Springsteen") p.save(force_insert=True) 

are equivalent.

The force_insert parameter is documented elsewhere, but all it means is that a new object will always be created. Normally you won’t need to worry about this. However, if your model contains a manual primary key value that you set and if that value already exists in the database, a call to create() will fail with an IntegrityError since primary keys must be unique. Be prepared to handle the exception if you are using manual primary keys.

The Django documentation says it is the same. It is just more convenient to make it on one line. You could make a save() on one line too, but it would be more verbose and less readable -- it is clear you are creating a new object with the create() method.

create(**kwargs)

A convenience method for creating an object and saving it all in one step. Thus:

p = Person.objects.create(first_name="Bruce", last_name="Springsteen") 

and:

p = Person(first_name="Bruce", last_name="Springsteen") p.save(force_insert=True) 

are equivalent.

The force_insert parameter is documented elsewhere, but all it means is that a new object will always be created. Normally you won’t need to worry about this. However, if your model contains a manual primary key value that you set and if that value already exists in the database, a call to create() will fail with an IntegrityError since primary keys must be unique. Be prepared to handle the exception if you are using manual primary keys.

Source Link
Maxime Lorant
  • 36.4k
  • 19
  • 91
  • 97

The documentation says it is the same. It is just more convenient to make it on one line. You could make a save() on one line too, but it would be more verbose and less readable: it is clear you are creating a new object with the create() method.

create(**kwargs)

A convenience method for creating an object and saving it all in one step. Thus:

p = Person.objects.create(first_name="Bruce", last_name="Springsteen") 

and:

p = Person(first_name="Bruce", last_name="Springsteen") p.save(force_insert=True) 

are equivalent.

The force_insert parameter is documented elsewhere, but all it means is that a new object will always be created. Normally you won’t need to worry about this. However, if your model contains a manual primary key value that you set and if that value already exists in the database, a call to create() will fail with an IntegrityError since primary keys must be unique. Be prepared to handle the exception if you are using manual primary keys.