我不能让它为我的生活工作。
我在 api.py 中有这个
class catResource(ModelResource):
class Meta:
queryset = categories.objects.all()
resource_name = 'categories'
allowed_methods = ['get', 'post', 'put']
authentication = Authentication()
所以当我尝试时:
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"name":"Test", "parent": "0", "sort": "1","username":"admin","password":"password"}' http://192.168.1.109:8000/api/v1/categories/
我得到:
HTTP/1.0 401 UNAUTHORIZED
Date: Sat, 21 Sep 2013 10:26:00 GMT
Server: WSGIServer/0.1 Python/2.6.5
Content-Type: text/html; charset=utf-8
该模型:
class categories(models.Model):
name = models.CharField(max_length=255)
parent = models.ForeignKey('self', blank=True,null=True)
sort = models.IntegerField(default=0)
def __unicode__(self):
if self.parent:
prefix = str(self.parent)
else:
return self.name
return ' > '.join((prefix,self.name))
@classmethod
def re_sort(cls):
cats = sorted([ (x.__unicode__(),x) for x in cls.objects.all() ])
for i in range(len(cats)):
full_name,cat = cats[i]
cat.sort = i
super(categories,cat).save()
def save(self, *args, **kwargs):
super(categories, self).save(*args, **kwargs)
self.re_sort()
class Admin:
pass