简体   繁体   English

保存前编辑 django-rest-framework 序列化程序 object

[英]Editing django-rest-framework serializer object before save

I want to edit a django-rest-framwork serializer object before it is saved.我想在保存之前编辑一个django-rest-framwork序列化程序 object。 This is how I currently do it -这就是我目前的做法 -

def upload(request):
    if request.method == 'POST':
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid(): # All validation rules pass
             obj = form.save(commit=False)
             obj.user_id = 15
             obj.save()

How can I do it with a django-rest-framework serializer object?我如何使用django-rest-framework序列化器 object 来实现?

@api_view(['POST','GET'])
def upload_serializers(request):
    if request.method == 'POST':
         serializer = FilesSerializer(data=request.DATA, files=request.FILES)
         if serializer.is_valid():
              serializer.save()

Now edited for REST framework 3 现在为REST框架3编辑

With REST framework 3 the pattern is now: 使用REST框架3,模式现在是:

if serializer.is_valid():
    serializer.save(user_id=15)

Note that the serializers do not now ever expose an unsaved object instance as serializer.object , however you can inspect the raw validated data as serializer.validated_data . 需要注意的是串行现在暴露过未保存的对象实例作为serializer.object ,但是你可以检查原始验证数据serializer.validated_data

If you're using the generic views and you want to modify the save behavior you can use the perform_create and/or perform_update hooks... 如果您正在使用通用视图并且想要修改保存行为,则可以使用perform_create和/或perform_update挂钩...

def perform_create(self, serializer):
    serializer.save(user_id=15)

You can edit the serializer's object before save the serializer: 您可以在保存序列化程序之前编辑序列化程序的对象:

if serializer.is_valid():
    serializer.object.user_id = 15 # <----- this line
    serializer.save()

Just in case you need to modify the data before validation, there is another option without needing to override the functions create or perform_create .以防万一您需要在验证之前修改数据,还有另一种选择,无需覆盖函数createperform_create Just use the to_internal_value function on your serializers class:只需在序列化程序 class 上使用to_internal_value function:

class MySerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
    def to_internal_value(self, data):
        data['field1'] = "new value for field 1"
        data['field2'] = "new value for field 2"
        data['field3'] = "new value for field 3"
        return super().to_internal_value(data)

So we can modify the data before doing the validation.所以我们可以在进行验证之前修改数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django-rest-framework 中的可写嵌套序列化程序? - Writable nested serializer in django-rest-framework? Django-Rest-Framework中序列化器的问题 - Issues with Serializer in Django-Rest-Framework 如何编写django-rest-framework序列化程序以保存包含通用模型的嵌套层次结构? - How do I write a django-rest-framework serializer to save a nested hierarchy containing a generic model? 如何在 django-rest-framework 的序列化器中使用时区序列化时间? - how to serialize time with timezone in django-rest-framework's serializer? 覆盖 Django-Rest-Framework 序列化程序 is_valid 方法 - Overriding Django-Rest-Framework serializer is_valid method django-rest-framework 如何使模型序列化器字段成为必需 - django-rest-framework how to make model serializer fields required 根据django-rest-framework序列化器中的请求显示字段 - Display fields based on the request in django-rest-framework serializer 将动态字段添加到Django-rest-framework中的序列化器 - Adding dynamic fields to a serializer in django-rest-framework 使用序列化程序类 django-rest-framework 插入数据 - Insert data using serializer class django-rest-framework 如何在django-rest-framework中将参数传递给序列化器? - How to pass arguments to a Serializer in django-rest-framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM