gpt4 book ai didi

python - 非模型对象上的 Django Rest Framework 3 序列化程序?

转载 作者:太空狗 更新时间:2023-10-29 17:46:39 26 4
gpt4 key购买 nike

我正在从 2.4 升级到 DRF3.1.1。我正在使用自定义序列化程序来创建不是模型的对象的实例。

在 2.4 中,这样做很容易,因为在序列化程序中,我会在 restore_object() 中创建对象。在 View 中,我将调用 serializer.is_valid(),然后使用 serializer.object 将对象的实例从序列化程序中弹出。然后我可以为所欲为。

随着 3.x 的变化,从对象中获取实例变得更加困难,因为创建和更新方法应该进行保存,而“serializer.object”不再可用。

例如,我曾经将此用于我的“UserRegistration”对象。这不是一个模型,因为它是一个方便的对象,服务器解析并将数据存储在许多其他对象/数据库表中。

class UserRegistration(object):
def __init__(self, full_name, stage_name, password="", email="", locale="en_US"):
self.full_name = full_name
self.password = password
self.locale = locale
self.email = email
self.stage_name = stage_name

这是关联的 DRF-2.4 序列化程序:

class UserRegistrationSerializer(serializers.Serializer):
full_name = serializers.CharField(max_length=128, required=False)
stage_name = serializers.CharField(max_length=128)
password = serializers.CharField(max_length=128, required=False)
locale = serializers.CharField(max_length=10, required=False)
# use CharField instead of EmailField for email. We do our own validation later to make for a better error msg.
email = serializers.CharField(max_length=254, required=False)

def restore_object(self, attrs, instance=None):
if instance is not None:
instance.full_name = attrs.get('full_name', instance.full_name)
instance.password = attrs.get('password', instance.password)
instance.locale = attrs.get('locale', instance.locale)
instance.email = attrs.get('email', instance.email)
instance.stage_name = attrs.get('stage_name', instance.stage_name)
return instance
return UserRegistration(**attrs)

然后在我看来,我会做这样的事情:

class UserRegistration(APIView):
throttle_classes = ()
serializer_class = UserRegistrationSerializer

def post(self, request, format=None):
event_type = "user_registration"
serializer = UserRegistrationSerializer(data=request.DATA, context={'request': request})
try:
if serializer.is_valid():
user_registration = serializer.object
# save user_registration pieces in various places...

但是,在DRF3中,我的serializer.object没有了。文档说使用 serializer.validated_data 进行“验证”,但这只是一个散列,而不是真正的对象。有没有办法得到对象?

整个事情似乎更像是与 DB 对象结合,在这种特殊情况下,这正是我试图避免的。

我是不是错过了一些新的 DRF3 概念?

最佳答案

感谢@levi 开始回答,但不幸的是,这还不是全部,所以我认为这是一个更完整的回答。

我最初问的是:

Am I just missing some new DRF3 concept?

原来……是的。我曾是。文档讨论了新的单步对象创建,这让我觉得序列化和模型已经变得更加紧密。这种想法是不正确的,因为如果您编写自己的自定义序列化程序,则由您在新的 serializer.update() 中执行实际的对象保存(或不)和 serializer.create() 方法。

我也问过:

In 2.4, it was easy enough to do this because in the serializer, I would create the object in restore_object(). In the view, i'd call serializer.is_valid() and then pop the instance of the object out of the serializer with serializer.object. Then I could do whatever I want.

With the 3.x changes, it's harder to get the instance out of the object because the create and update methods are supposed to do the saving, and "serializer.object" isn't available anymore.

尽管在调用 serializer.is_valid() 之后没有可用于将创建的对象拉出的 serializer.objectserializer.save( ) 方法返回对象本身,在我的例子中这很好。

事实证明,代码更改根本不是很大。这是我对 DRF-3 非常满意的新代码:

class UserRegistration(object):
def __init__(self, full_name, stage_name, password="", email="", locale="en_US", notification_pref="ask"):
self.full_name = full_name
self.password = password
self.locale = locale
self.email = email
self.stage_name = stage_name


class UserRegistrationSerializer(serializers.Serializer):
full_name = serializers.CharField(max_length=128, required=False)
stage_name = serializers.CharField(max_length=128)
password = serializers.CharField(max_length=128, required=False)
locale = serializers.CharField(max_length=10, required=False)
# use CharField instead of EmailField for email. We do our own validation later to make for a better error msg.
email = serializers.CharField(max_length=254, required=False)

def update(self, instance, validated_data):
instance.full_name = validated_data.get('full_name', instance.full_name)
instance.password = validated_data.get('password', instance.password)
instance.locale = validated_data.get('locale', instance.locale)
instance.email = validated_data.get('email', instance.email)
instance.stage_name = validated_data.get('stage_name', instance.stage_name)
return instance

def create(self, validated_data):
return UserRegistration(**validated_data)

请注意,在序列化程序中没有将对象保存到任何数据库。我只是创建或更新对象,然后返回它。

现在 View 看起来像这样:

class UserRegistration(APIView):
throttle_classes = ()
serializer_class = UserRegistrationSerializer

def post(self, request, format=None):
event_type = "user_registration"
serializer = UserRegistrationSerializer(data=request.DATA, context={'request': request})
try:
if serializer.is_valid():
user_registration = serializer.save()
# save user_registration pieces in various places...

我在原帖中也说过:

The whole thing seems more married to DB objects, which in this particular case is exactly what i'm trying to avoid.

从创建和更新方法不必将任何内容保存到任何数据库这一事实可以看出,该声明也是不正确的。

这里需要注意的是代码是有效的,但显然我只是在思考 DRF2.x->3.x 的一些变化,所以我可以用非 DRF 的方式来做这件事。如果是这样,请知道的人随时告诉我如何做得更好。 :)

关于python - 非模型对象上的 Django Rest Framework 3 序列化程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29310000/

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