1. When the models object uses “all()”:
from django.core import serializers data_set = models.Areas.objects.all() res = serializers.serialize("json",data_set)
2. When the models object is a single value “get()”:
from django.forms.models import model_to_dict obj = models.Areas.objects.get(id=value) res = model_to_dict(obj)
3. The models object uses values or values_ List:
res={"status":True,"data":""} obj = models.Areas.objects.values("id","name") obj_list = list(obj) #Need to convert to a list, otherwise it will report an error res["data"] = obj_list return JsonResponse(res,safe=False)
Similar Posts:
- python Warning: OverflowError: Python int too large to convert to C long
- [Solved] Django REST Framwork Use HyperlinkedModelSerializer Error: `HyperlinkedIdentityField` requires the request in the serializer context
- Django auth.User.groups: (fields.E304) Reverse accessor for User.groups clashes with reverse
- Python AttributeError: ‘unicode’ object has no attribute ‘tzinfo’
- [Solved] DjangoORM Run python manage.py makemigrations Error: no changes detected
- PaddlePaddle Error: ‘map’ object is not subscriptable
- How to Solve Django xadmin installation Error [7 Types of Errors]
- How to Solve Django Error: No such column: xxx
- Python TypeError: __init__() missing 1 required positional argument: ‘on_delete’
- Convert Object to List>, avoiding Unchecked cast: ‘java.lang.Object’ to ‘java.util.List