Create Django model or update if exists

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I want to create a model object, like Person, if person's id doesn't not exist, or I will get that person object.

The code to create a new person as following:

class Person(models.Model):     identifier = models.CharField(max_length = 10)     name = models.CharField(max_length = 20)     objects = PersonManager()  class PersonManager(models.Manager):     def create_person(self, identifier):         person = self.create(identifier = identifier)         return person 

But I don't know where to check and get the existing person object.

回答1:

If you're looking for "update if exists else create" use case, please refer to @Zags excellent answer


Django already has a get_or_create, https://docs.djangoproject.com/en/1.9/ref/models/querysets/#get-or-create

For you it could be :

id = 'some identifier' person, created = Person.objects.get_or_create(identifier=id)  if created:    # means you have created a new person else:    # person just refers to the existing one 


回答2:

It's unclear whether your question is asking for the get_or_create method (available from at least Django 1.3) or the update_or_create method (new in Django 1.7). It depends on how you want to update the user object.

Sample use is as follows:

# In both cases, the call will get a person object with matching # identifier or create one if none exists; if a person is created, # it will be created with name equal to the value in `name`.  # In this case, if the Person already exists, its existing name is preserved person, created = Person.objects.get_or_create(         identifier=identifier, defaults={"name": name} )  # In this case, if the Person already exists, its name is updated person, created = Person.objects.update_or_create(         identifier=identifier, defaults={"name": name} ) 


回答3:

Django has support for this, check get_or_create

person, created = Person.objects.get_or_create(name='abc') if created:     # A new person object created else:     # person object already exists 


回答4:

Thought I'd add an answer since your question title looks like it is asking how to create or update, rather than get or create as described in the question body.

If you did want to create or update an object, the .save() method already has this behaviour by default, from the docs:

Django abstracts the need to use INSERT or UPDATE SQL statements. Specifically, when you call save(), Django follows this algorithm:

If the object’s primary key attribute is set to a value that evaluates to True (i.e., a value other than None or the empty string), Django executes an UPDATE. If the object’s primary key attribute is not set or if the UPDATE didn’t update anything, Django executes an INSERT.

It's worth noting that when they say 'if the UPDATE didn't update anything' they are essentially referring to the case where the id you gave the object doesn't already exist in the database.



回答5:

If one of the input when you create is a primary key, this will be enough:

Person.objects.get_or_create(id=1) 

It will automatically update if exist since two data with the same primary key is not allowed.



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