我试图将一些数据发送给tastypie poc。
我设计了我的模型:
class Category(models.Model):
name = models.CharField(max_length=30)
icon = models.FileField(upload_to="media/img/wish-icons/")
class Wish(models.Model):
content = models.CharField(max_length=140)
category = models.ManyToManyField('wishes.Category', blank=True, null=True)
lat = models.FloatField('Latitude', blank=True, null=True)
lon = models.FloatField('Longitude', blank=True, null=True)
我有一个相关的api.py:
class CategoryResource(ModelResource):
class Meta:
queryset = Category.objects.all()
resource_name = 'category'
fields = ['name', 'icon']
class WishResource(ModelResource):
category = fields.ToManyField(CategoryResource, 'category', full=True)
class Meta:
queryset = Wish.objects.all()
resource_name = 'wish'
authorization = Authorization()
list_allowed_methods = ['get', 'post']
我可以发送GET请求来获取列表或详细信息但是当我想用jQuery和ajax发送数据时,Firebug会返回401错误。
我确实喜欢示例here,但出了点问题,我不知道在哪里。这不是跨域请求,前端和tastypie必须有相同的来源。
我的ajax请求如下所示:
var data = JSON.stringify({
"content": "This will prbbly be my lst post."
});
$.ajax({
url: 'http://127.0.0.1:8000/api/v1/wish/',
type: 'POST',
contentType: 'application/json',
data: data,
dataType: 'json',
processData: false
});
答案 0 :(得分:0)
class CategoryResource(ModelResource):
class Meta:
queryset = Category.objects.all()
resource_name = 'category'
authorization = Authorization()
class WishResource(ModelResource):
categories = fields.ToManyField('wishes.api.CategoryResource', 'category', null=True, full=True)
class Meta:
queryset = Wish.objects.all()
resource_name = 'wish'
authorization = Authorization()