简体   繁体   中英

how get only fields in django serializers

i used django.core.serializers to get data from model as below code

from django.core import serializers
...
serializers.serialize('json',production_time.production_items.all())

and i get response as below:

[{"model": "BorseBots.exchangemonthlyproductionitem", "pk": 1308, "fields": {'name':'jack',...}} , ...]

but i do not need 'model' and 'pk' fields so expect below:

[{'name':'jack',...} , ...]

or

[{"fields": {'name':'jack' , ...}} , ...]

how get this?

need use loop to extract data in new list?

using django2.2.* python3.8

Django's rest framework make it possible to format your serializer as you prefer. You can extract any fields or even get values from model methods.

serializers.py:

from rest_framework import serializers
from .models import YourObject


class YourObjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = YourObject
        fields = ['id', 'name', ...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM