希望您一切顺利。我需要对我的原始SQL查询进行序列化:
SELECT nn.*,nm.*FROM notifications_newsletter nn LEFT JOIN notifications_message nm ON nn.id=nm.newsletter_id_id ORDER by nm.status DESC
models.py
from django.db import models
class Newsletter(models.Model):
start_datetime = models.DateTimeField()
text = models.TextField(blank=True)
filter = models.ForeignKey('Filter', null=True, on_delete=models.SET_NULL)
end_datetime = models.DateTimeField()
class Message(models.Model):
send_datetime = models.DateTimeField('%Y-%m-%d %H:%M:%S', auto_now=True)
status = models.BooleanField(default=False)
newsletter_id = models.ForeignKey('Newsletter', on_delete=models.CASCADE)
client_id = models.ForeignKey('Client', on_delete=models.CASCADE)
views.py
from django.core.serializers import serialize
from django.http import HttpResponse
from .models import Newsletter
def some_view(request):
sql = 'SELECT nn.*,nm.*FROM notifications_newsletter nn ' \
'LEFT JOIN notifications_message nm ' \
'ON nn.id=nm.newsletter_id_id ORDER by nm.status DESC'
qs = Newsletter.objects.raw(sql)
qs_json = serialize('json', qs)
return HttpResponse(qs_json, content_type='application/json')
如果我使用
serializers.serialize()
进行操作,那么响应中所有连接数据(消息表)都不存在。但是,如果打印print(qs.columns)
,则会打印列send_datetime, status, etc.。响应:
[
{
"model": "notifications.newsletter",
"pk": 42,
"fields": {
"start_datetime": "2022-01-21T21:56:09Z",
"text": "This is test message for 900 operator code.",
"filter": 1,
"end_datetime": "2022-01-21T18:00:00Z"
}
},
]
我需要类似于以下的东西:
[
{
"model": "notifications.newsletter",
"pk": 43,
"fields": {
"start_datetime": "2022-01-21T22:03:26Z",
"text": "This is test message for 904 operator code.",
"filter": 2,
"end_datetime": "2022-01-21T18:00:00Z",
"messages": [
{
"send_datetime": "2022-01-21T22:03:26Z",
"status": 0,
"newsletter_id": 43,
"client_id": 1
},
]
}
},
]
它是否可以被正常序列化?
qs_json = NewsletterSerializer(qs, many=True).data
返回的是Return List
对象。有没有可能返回JSON? - barelydrownJSON
对象而不是list
,可以尝试使用json.dumps(NewsletterSerializer(qs, many=True).data)
。 - Oleksii Tambovtsevjson.dumps
将返回一个json字符串。要获取适当的对象,请改用json.loads(json.dumps(NewsletterSerializer(qs, many=True).data))
。 - ImportError