What's the best way to create a model object in Django?

django, django-models, django-views

Solution

`create()` is like a wrapper over `save()` method.

create(**kwargs)

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

Django 1.8 source code for `create()` function:

def create(self, **kwargs):
        """
        Creates a new object with the given kwargs, saving it to the database
        and returning the created object.
        """
        obj = self.model(**kwargs)
        self._for_write = True
        obj.save(force_insert=True, using=self.db) # calls the `save()` method here
        return obj

For `create()`, a `force_insert` parameter is passed while calling `save()` internally which forces the `save()` method to perform an SQL `INSERT` and not perform an `UPDATE`. It will forcibly insert a new row in the database.

For `save()`, either an `UPDATE` or `INSERT` will be performed depending on the object’s primary key attribute value.

Problem

``` Author.objects.create(name="Joe") ``` or ``` an_author = Author(name="Joe") an_author.save() ``` What's the difference between these two? Which one is better? Similar questions: - difference between objects.create() and object.save() in django orm - Django: Difference between save() and create() from transaction perspective

Original source

Related problems