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

て烟熏妆下的殇ゞ 提交于 2019-12-04 11:31:03

问题


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


回答1:


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.




回答2:


Create is just a convenience proxy for creating a new object with kwargs. As you can see below, it calls save() for you:

From Django Source:

 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)
        return obj

One thing to notice is the force_insert argument to save. This means that Django will always use an INSERT sql statement here and not an UPDATE. The default is false, so in your second example save() will INSERT or UPDATE.




回答3:


The first one you are using the Manager method create. It already implemented for you and it will save automatically.

The second method you are creating an instance of class Author then you are calling save.

So in conclusion,

Author.objects.create(name="Joe") create --> save()

the other one first line do create, and second line do save.


in some cases, you will need to call the manager method always. For example, you need to hash the password.

# In here you are saving the un hashed password. 

user = User(username="John")
user.password = "112233"
user.save()


# In here you are using the manager method, 
# which provide for you hashing before saving the password. 

user = User.objects.create_user(username="John", password="112233")

So basically, in your models think about it as setters. If you want to modify the data always while creation then use managers.



来源:https://stackoverflow.com/questions/31624075/whats-the-best-way-to-create-a-model-object-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!