drf 自定义响应_在DRF中正确定义SerializerMethodFields

news/2024/7/10 0:46:36 标签: java, spring, python, django, vue

drf 自定义响应

I’m currently implementing a RESTful API with Django Rest Framework (a fantastic framework by the way). I’m mostly depending on the automatic serialization that a ModelSerializer provides.

我目前正在使用Django Rest Framework (顺便说一句很棒的框架)实现RESTful API。 我主要取决于ModelSerializer提供的自动序列化。

 class class ReportSerializerReportSerializer (( serializersserializers .. ModelSerializerModelSerializer ):
    ):
    class class MetaMeta :
        :
        model model = = modelsmodels .. Report
        Report
        fields fields = = (( 'id''id' , , 'name''name' )
)

But sometimes you need extra fields. In my case I wanted to add a readonly image_url field to a serializer. So I added a SerializerMethodField:

但是有时您需要额外的字段。 就我而言,我想向序列化器添加一个只读的image_url字段。 所以我添加了SerializerMethodField

This looks correct at a first glance, however, my API server would now throw exceptions like this one: ImproperlyConfigured at /api/v1/reports/2 - Field name `logo_url` is not valid for model `ModelBase`.

乍看之下这看起来是正确的,但是,我的API服务器现在会抛出类似这样的异常: 在/ api / v1 / reports / 2中配置不正确-字段名称logo_url对于模型ModelBase无效。

ImproperlyConfigured at /api/v1/reports/2 - Field name `logo_url` is not valid for model `ModelBase`.

At first I thought it was a naming conflict with a model field or method. But then, the solution turned out to be much simpler: In a moment of mental aberration I must have defined an attribute and a method with the same name.

起初,我认为这是与模型字段或方法的命名冲突。 但是,结果却变得简单得多:在出现精神异常时,我必须定义了一个具有相同名称的属性和方法。

The solution is to use the get_ prefix for the method name:

解决方案是对方法名称使用get_前缀:

 class class ReportSerializerReportSerializer (( serializersserializers .. ModelSerializerModelSerializer ):
    ):
    logo_url logo_url = = serializersserializers .. SerializerMethodFieldSerializerMethodField ()

    ()

    class class MetaMeta :
        :
        model model = = modelsmodels .. Report
        Report
        fields fields = = (( 'id''id' , , 'name''name' , , 'logo_url''logo_url' )

    )

    def def get_logo_urlget_logo_url (( selfself , , objobj ):
        ):
        return return objobj .. logologo .. imageimage .. url
url

翻译自: https://www.pybloggers.com/2015/02/properly-defining-serializermethodfields-in-drf/

drf 自定义响应


http://www.niftyadmin.cn/n/984050.html

相关文章

翻翻git之---偏向iOS风格的Switch ToggleSwitch

转载请注明出处:王亟亟的大牛之路 早上上了个很6的图片选择器 GalleryFinal 如果没有看的同学 可以看下,东西很不错哦,传送门:http://blog.csdn.net/ddwhan0123/article/details/50817152 这一篇上一个自定义控件ToggleSwitch 效…

基于阿里云HBase搭建OpenTSDB

OpenTSDB是一个基于HBase的分布式、可伸缩的开源时序数据库。OpenTSDB由TSD(Time Series Daemon)和一系列命令行工具组成。TSD用于接收用户请求并将时序数据存储在HBase中。TSD之间是相互独立的,没有master,也没有共享状态&#x…

Python社区专访Mike Grouchy

If you saw the last Python Community Interview with Mahdi Yusuf, then you have already met one half of the Pycoder’s Weekly team. This time, I’m joined by Mahdi’s partner in crime, Mike Grouchy. 如果您上一次对Mahdi Yusuf进行了Python社区采访,那…

Mono for Android 优势与劣势

原文:Mono for Android 优势与劣势最近有兴趣了解一下Mono for Andriod,也就是使用.NET平台来开发Andriod程序。Mono for Android API 几乎映射标准的Andriod API.例如,两边API几乎所有相同的类、方法和字段都相同。主要区别之一,就是Mono fo…

ios swift学习日记1-Swift 初见

最近ios的swift语言似乎火了,我没有objectc基金会,但在此之前有cjava的基础的。从这几天開始学习ios的swift语言。后期以博客形式公布。这里提供一本翻译的英文版的swif书籍。还是非常好地。本人分享资源从来不要积分哦。仅仅求给大家提供帮助 http://do…

Android 将数据库文件保存至sdcard中

2019独角兽企业重金招聘Python工程师标准>>> Android 将数据库文件保存至sdcard中,实现工具类: import android.content.Context; import android.os.Environment; import android.util.Log;import java.io.File; import java.io.FileInputSt…

MAC系统如何下载安装Homebrew和git

MAC系统如何下载安装Homebrew和git 安装Homebrew: Homebrew进入官网后复制里面的安装代码在命令行中输入: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"注意:国内大部分区…

vim python_VIM和Python –天生一对

vim pythonIt has come to my attention that somebody ‘round these parts has been preaching the gospel of Sublime Text 3. Well, as the resident senior developer (err, old fogey), I feel it’s my duty to provide a counterexample by way of the only TRUE Pytho…