对不起,我是django和python的初学者,我创建了一个项目,我有一个这样的models.py:
from django.db import models
class Shoes(models.Model):
type = models.CharField(max_length=30)
start_date = models.DateTimeField()
number = models.IntegerField()
def __unicode__(self):
return str(self.id)
class Meta:
verbose_name_plural = "Shoes"
class Bottom(models.Model):
type = models.CharField(max_length=30)
finish = models.BooleanField()
size = models.IntegerField()
def __unicode__(self):
return str(self.id)
class Meta:
verbose_name_plural = "Bottoms"
class Relation(models.Model):
shoes = models.OneToOneField(Shoes)
bottom = models.ForeignKey(Bottom)
class Meta:
verbose_name_plural = "Relations"我想在json..sorry中序列化这些类,我需要了解在哪里以及如何编写特定的代码来实现这一点。我已经写了一个文件views.py和一个file.html来查看带有这些对象表的网页,但是现在因为我需要写一个jquery函数,当我添加一个新对象时允许自动更新网页,我认为在这样做之前我们需要在json中序列化数据。谢谢,如果我说了一些愚蠢的话,请容忍我,因为我在这个领域是一个真正的初学者。
发布于 2012-04-24 19:14:23
你想序列化类吗?您想序列化对象!:)为了序列化Django对象,您可以使用内置机制。请阅读以下内容:
https://docs.djangoproject.com/en/dev/topics/serialization/
例如,您可以这样做:
from django.core import serializers
from django.http import HttpResponse
def someView(request):
shoes_from_db = Shoes.objects.all()
json = serializers.serialize(
'json', shoes_from_db, fields=('type','start_date', 'number')
)
return HttpResponse(json, content_type="application/json")https://stackoverflow.com/questions/10294866
复制相似问题