0
测试其期望得到所有用户列表的GET请求时,我下面有点过时tutorialand我得到一个错误:“查询集”对象有没有属性“追加”
'QuerySet' object has no attribute 'append'
下面是用户配置模式。
class UserProfile(models.Model):
user = models.OneToOneField(User)
nickname = models.CharField(max_length = 128)
comment = models.TextField()
country = models.CharField(max_length = 128, blank = True)
url = models.CharField(max_length = 128, blank = True)
ignores = models.ManyToManyField(User, related_name = 'ignore_set', blank = True, null = True)
def serialize(self):
data = {
'user':self.user_id,
'username':self.user.username,
'nickname':self.nickname,
'comment':self.comment,
'country':self.country,
'url':self.url,
'ignores':[],
}
return data
下面
是view.py(请参阅API /用户/列表)
def user_view(request, method):
# api/user/create: api that creating new account.
if method == 'create' and request.method == 'POST':
...
#api/user/update: api that update user's oldpassword with new one
elif method == 'update' and request.method == 'POST':
...
elif method == 'list':
users = UserProfile.objects.all()
return toJSON(serial(users))
else:
return toJSON({'status':'bad request'},400)
我想下面的功能序列化是问题的根源..但不知道如何解决它..
def serial(objs):
serialized = []
for obj in objs:
objs.append(obj.serialize())
return serialized
def toJSON(objs, status=200):
j = json.dumps(objs, ensure_ascii=False)
return HttpResponse(j, status=status, content_type='application/json; charset=utf-8')
注意:您可能希望找到一个更好的教程(Django的官员是相当不错的),然后也许看看Django的REST框架 – Sayse
这确实是一个基本的错误..是的,我一定会采取django官方教程,非常感谢您的帮助。 –