我正在尝试使用Django中的Tastypie Api更新对象,但我找不到任何方法来执行此操作...
我想发送一个像/ api / vote / pk = 3这样的网址,它会用pk = 3更新对象并增加投票编号字段......是否可以轻松完成?
先谢谢...
我只是尝试创建一个像这样的Ressource:但即使我不做任何处理它也不起作用。它需要定义obj_get_list等...
class IphoneVoteRessource(Resource):
id = fields.IntegerField(attribute='id')
name = fields.CharField(attribute='name')
class Meta:
resource_name = 'poi_vote'
object_class = Row
def obj_update(self, bundle, request=None, **kwargs):
# update an existing row
pk = int(kwargs['pk'])
return bundle `
我已经多次使用Tastypie获取数据,但从不更新一个对象..
答案 0 :(得分:0)
我终于使用了ModelResource,如果有人想要一个有效的例子:
class VoteResourceAnonymous(ModelResource):
class Meta:
queryset = VoteAnonymous.objects.all()
resource_name = 'vote_object'
# need to send 'vote_value' and 'object_id'
excludes = ['created_at', 'content_type', 'content_object', 'user_agent', 'ip_address']
allowed_methods = ['get', 'post', 'put', 'delete']
always_return_data = True
def obj_create(self, bundle, request=None, **kwargs):
vote_value = bundle.data['vote_value']
object_id = bundle.data['object_id']
tmp_poi = PointOfInterestActivity.objects.get(id=object_id)
content_type_object = ContentType.objects.get_for_model(tmp_poi)
bundle.obj = VoteAnonymous(vote_value=vote_value,
object_id=object_id,
user_agent="",
ip_address="",
content_type=content_type_object,
)
bundle.obj.save()
if vote_value > 0:
bundle.data['new_nb_votes'] = tmp_poi.nb_votes + 1
else:
bundle.data['new_nb_votes'] = tmp_poi.nb_votes - 1
return bundle