how can I add field in serializer?

2024/11/17 13:49:24

Below is my serializer.py file:

from rest_framework import serializersclass TaskListSerializer(serializers.Serializer):id = serializers.CharField()user_id = serializers.CharField()status = serializers.CharField()name = serializers.CharField()

In Python shell, I input this:

>>> from serializer import TaskListSerializer as ts>>> result = ts({'id':1, 'user_id': 1, 'status':2, 'name': 'bob'})>>> result.data{'status': u'2', 'user_id': u'1', 'id': u'1', 'name': u'bob'}  

Now I want do this:

First, the input is not change, is also {'id':1, 'user_id': 1, 'status':2, 'name': 'bob'}

But I want to add a field and change name: bob to jim in serializer.py and make output like this:

{'status': u'2', 'user_id': u'1', 'id': u'1', 'name': u'jim', u'age': '15'}

How can I do it in serializer.py?

Answer

Use serializers.SerializerMethodField()

class TaskListSerializer(serializers.ModelSerializer):
complex_things = serializers.SerializerMethodField()def get_complex_things(self, obj):result_of_complex_things = 2 + 2return result_of_complex_things
https://en.xdnf.cn/q/120195.html

Related Q&A

Read and write a variable in module A from module B

Need a solution or workaround to read and write the variable in moduleA from moduleB please. Here is the code:moduleAimport moduleBvariable = 10def changeVariable():global variablevariable = 20def main…

Iterate through a pandas dataframe to get a specific output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 1 year ago.The comm…

Detect if you click inside a box in Python Zelle graphics

I have created a dice betting game. When my code is run, there is a "CLICK TO ROLL" button. Currently, if you click anywhere on the screen, the dice will roll. How can I make it so the progra…

Nested Triangle in Python

My assingmentAt each level the complete triangle for the previous level is placed into an extra outer triangle. The user should be asked to input the two characters to be used and the width of the inne…

How to fix cannot open file Test.py: [Errno2] No such file or directory?

I tried to run a python script which does not exist in current folder, for exampleC:\>python Test.pypython:cant open file Test.py:[Errno2] No such file or directoryI have to specify the absolute pat…

Highlight cells in a column in google spreadsheet when the value above a threshold with python

Here is a simplified example of my codes and the screenshot of the results I want to get in google spreadsheet. I hope to either save the dataframe style to google spreadsheet as applying table style t…

cannot concatenate str and file objects : Python error

I have Following piece of code: for src_filename, src_code in src_dict.iteritems(): try: set.dependencies = subprocess.check_output(unifdef -s /home/c/maindir/folder/ +src_filename, shell=True) except…

Run python script from html button submit

i have a code input data to txt file :<form action="proses.php" method="post">Nomor Polisi : <br><input type="text" name="nopol"><br><…

Reading log files in python

I have a log file (it is named data.log) containing data that I would like to read and manipulate. The file is structured as follows:#Comment line 1 #Comment line 2 1.00000000,3.02502604,343260.6865…

Return more than one value in python function [duplicate]

This question already has answers here:How can I use `return` to get back multiple values from a loop? Can I put them in a list?(2 answers)Closed 1 year ago.I was trying to use *args with a for loop …