我有这个“模型”
class messages(models.Model):
status_choices = (
(u'read',u'read'),
(u'unread',u'unread')
)
user = models.ForeignKey(User)
message = models.TextField()
status = models.CharField(max_length=6,choices=status_choices,default='unread')
sender = models.ForeignKey(User,related_name="sender")
这是我的观点,它获取用户消息并将其传递给 javascript。
@login_required
def ajax_fetch_messages(request):
if request.is_ajax():
print(request.POST['user'])
# first check that user himself asked for his/her messages
requested_user = get_object_or_404(User,id=request.POST['user'])
if requested_user == request.user:
# do processing of messages
user_messages = messages.objects.filter(user=requested_user,status='unread')
'''try:
response = dumps(messages)
except:
print("Unable to dump messages")'''
return HttpResponse(user_messages)
else:
return HttpResponse("You're not authorised to do that")
else:
return HttpResponse("This method can't be called this way")
现在我有以下担忧
- 我无法对上述视图返回的对象进行 jsonify。所以我必须将视图返回的“消息”对象发送到 javascript。
- 将 python 对象传递给 javascript 而不在 django 端对对象进行 jsonfying 是否安全?如果不是,那么可能出现的问题是什么?
那么,你对我的问题有什么想说的吗?