Suppose I have a model like this:
假设我有一个这样的模型:
class Book(models.Model):
num_pages = ...
author = ...
date = ...
Can I create a dictionary, and then insert or update the model using it?
我可以创建一个字典,然后使用它插入或更新模型吗?
d = {"num_pages":40, author:"Jack", date:"3324"}
3 个解决方案
#1
71
Here's an example of create using your dictionary d:
这里有一个使用字典d创建的例子:
Book.objects.create(**d)
To update an existing model, you will need to use the QuerySet filter
method. Assuming you know the pk
of the Book you want to update:
要更新现有的模型,需要使用QuerySet过滤器方法。假设你知道你想要更新的书的pk:
Book.objects.filter(pk=pk).update(**d)
#2
41
If you know you want to create it:
如果你知道你想创造它:
Book.objects.create(**d)
Assuming you need to check for an existing instance, you can find it with get or create:
假设您需要检查现有的实例,您可以使用get或create:
instance, created = Book.objects.get_or_create(slug=slug, defaults=d)
if not created:
for attr, value in d.iteritems():
setattr(instance, attr, value)
instance.save()
As mentioned in another answer, you can also use the update
function on the queryset manager, but i believe that will not send any signals out (which may not matter to you if you aren't using them). However, you probably shouldn't use it to alter a single object:
正如在另一个答案中所提到的,您也可以在queryset manager上使用update函数,但是我相信它不会发送任何信号(如果您不使用它们,这对您来说可能不重要)。但是,您可能不应该使用它来修改单个对象:
Book.objects.filter(id=id).update()
#3
26
Use **
for creating a new model. Loop through the dictionary and use setattr()
in order to update an existing model.
使用**创建新模型。遍历字典并使用setattr()来更新现有模型。
From Tom Christie's Django Rest Framework
来自Tom Christie的Django Rest框架
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py
for attr, value in validated_data.items():
setattr(instance, attr, value)
instance.save()
#1
71
Here's an example of create using your dictionary d:
这里有一个使用字典d创建的例子:
Book.objects.create(**d)
To update an existing model, you will need to use the QuerySet filter
method. Assuming you know the pk
of the Book you want to update:
要更新现有的模型,需要使用QuerySet过滤器方法。假设你知道你想要更新的书的pk:
Book.objects.filter(pk=pk).update(**d)
#2
41
If you know you want to create it:
如果你知道你想创造它:
Book.objects.create(**d)
Assuming you need to check for an existing instance, you can find it with get or create:
假设您需要检查现有的实例,您可以使用get或create:
instance, created = Book.objects.get_or_create(slug=slug, defaults=d)
if not created:
for attr, value in d.iteritems():
setattr(instance, attr, value)
instance.save()
As mentioned in another answer, you can also use the update
function on the queryset manager, but i believe that will not send any signals out (which may not matter to you if you aren't using them). However, you probably shouldn't use it to alter a single object:
正如在另一个答案中所提到的,您也可以在queryset manager上使用update函数,但是我相信它不会发送任何信号(如果您不使用它们,这对您来说可能不重要)。但是,您可能不应该使用它来修改单个对象:
Book.objects.filter(id=id).update()
#3
26
Use **
for creating a new model. Loop through the dictionary and use setattr()
in order to update an existing model.
使用**创建新模型。遍历字典并使用setattr()来更新现有模型。
From Tom Christie's Django Rest Framework
来自Tom Christie的Django Rest框架
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py
for attr, value in validated_data.items():
setattr(instance, attr, value)
instance.save()