gpt4 book ai didi

python - 如何批量插入或创建多个数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:45 25 4
gpt4 key购买 nike

基于我在下面给出的给定数据对象。我想问一下如何在django中批量插入或创建多个数据。我在下面提供了包括我的序列化程序和 python 代码。我想从下面的数据对象中创建/插入每个密码。不要担心其他数据。我也想用序列化器来做。我确实尝试遍历 data: 中 each_employee_data 的数据。我的代码有任何问题。谢谢。

代码

def post(self, request, format=None):
data = request.data


print("Data::" , data)

for each_employee_data in data:
each_employee_data["verification_code"] = verification_code(self)
password = hashers.make_password(each_employee_data["password"])
each_employee_data["password"] = password
print("each_employee_data", each_employee_data)
user_serializer = UserSerializer(data= each_employee_data)

if user_serializer.is_valid(raise_exception=True):

if not each_employee_data["is_applicant"]:#company
company_serializer = CompanySerializer(data=data)
if company_serializer.is_valid(raise_exception=True):
user_ins = user_serializer.save(password=password)
company_serializer.save(user=user_ins)
else:
applicant_serializer = ApplicantSerializer(data=each_employee_data)
if applicant_serializer.is_valid(raise_exception=True):
user_ins = user_serializer.save(password=password)
applicant_serializer.save(user=user_ins)


send_confirmation.delay(data)
if not user_ins.is_applicant:
send_business_confirmation.delay(data)

序列化器

class UserSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
is_staff = serializers.BooleanField(required=False)
is_confirmed = serializers.BooleanField(required=False)
is_applicant = serializers.BooleanField(required=False)
email = serializers.EmailField(required=True,validators=[UniqueValidator(queryset=User.objects.all(), message="Email already exist.")])
verification_code = serializers.CharField(required=False)
password_reset_code = serializers.CharField(required=False)

def create(self, validated_data):
"""
Create and return a new `Snippet` instance, given the validated data.
"""
password = validated_data.pop("password")


return User.objects.create(password=password,**validated_data)

数据:

[  
{
'mname':'Tap',
'email':'helloworld@gmail.com',
'is_applicant':True,
'lname':'world',
'fname':'hello',
'password':'helloworld12345'
},
{
'mname':'van',
'email':'haha@gmail.com',
'is_applicant':True,
'lname':'asdas',
'fname':'hellworld124344',
'password':'asdasdasdas'
}
]

最佳答案

当一个序列化器被实例化并传入 many=True 时,一个 ListSerializer 实例将被创建。然后序列化器类成为父 ListSerializer 的子类。 ListSerializer 类提供了一次序列化和验证多个对象的行为。

在一些用例中,您可能希望自定义 ListSerializer 行为。在您的情况下,您希望 ListSerializer 进行批量更新。您需要定义一个新的自定义 ListSerializer

class CustomListSerializer(serializers.ListSerializer):
...

def create(self, validated_data):
...

class CustomSerializer(serializers.Serializer):
...
class Meta:
list_serializer_class = CustomListSerializer

你的情况应该是

class UserListSerializer(serializers.ListSerializer):
...

def create(self, validated_data):
user = [User(**item) for item in validated_data]
return User.objects.bulk_create(User)



class UserSerializer(serializers.Serializer):
...
class Meta:
list_serializer_class = UserListSerializer

https://www.django-rest-framework.org/api-guide/serializers/#customizing-listserializer-behavior

关于python - 如何批量插入或创建多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55788297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com