在tastypie和obj_create错误中反向关系“ToManyField”

时间:2013-06-14 02:07:32

标签: django tastypie

我在添加资源方面遇到了一些问题,由ApiKey用户关联,问题恰恰是“出租车”字段,当我发表评论时,“create_object”工作正常。 有资源

Resouces

class LocationResource(ModelResource):
    user = fields.ForeignKey(AccountResource, 'user', full=True)
    taxi = fields.ToManyField(TaxiResource, attribute=lambda bundle: Taxi.objects.filter(user=bundle.obj.user), full=True, null=True)

    class Meta:
        queryset = Location.objects.all().order_by('-id')
        resource_name = 'location'
        list_allowed_methods = ['post', 'get']
        authentication = ApiKeyAuthentication()
        authorization = Authorization()
        filtering = {'user': ALL_WITH_RELATIONS}

    def obj_create(self, bundle, **kwargs):
        if bundle.request.method == 'POST':
            return super(LocationResource, self).obj_create(bundle, user=bundle.request.user)

模型

from django.contrib.auth.models import User

class Taxi(models.Model):
    STATUS_CHOICES = (
        ('Av', 'Available'),
        ('NA', 'Not Available'),
        ('Aw', 'Away'),
    )
    user = models.OneToOneField(User)
    license_plate = models.TextField(u'Licence Plate',max_length=6,blank=True,null=True)
    status = models.CharField(u'Status',max_length=2,choices=STATUS_CHOICES)
    device = models.OneToOneField(Device, null=True)
    def __unicode__(self):
        return "Taxi %s for user %s" % (self.license_plate,self.user)


class Location(models.Model):
    user = models.ForeignKey(User)
    latitude = models.CharField(u'Latitude', max_length=25, blank=True, null=True)
    longitude = models.CharField(u'Longitude', max_length=25, blank=True, null=True)
    speed = models.CharField(u'Speed', max_length=25, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    def __unicode__(self):
        return "(%s,%s) for user %s" % (self.latitude, self.longitude,self.user)

我尝试创建一些资源时的错误是:

{"error_message": "'QuerySet' object has no attribute 'add'", "traceback": "Traceback (most recent call last):\n\n  File \"/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/tastypie/resources.py\", line 217, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/tastypie/resources.py\", line 459, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/tastypie/resources.py\", line 491, in dispatch\n    response = method(request, **kwargs)\n\n  File \"/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/tastypie/resources.py\", line 1357, in post_list\n updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))\n\n  File \"/Users/phantomis/Memoria/smartaxi_server/geolocation/api.py\", line 111, in obj_create\n    return super(LocationResource, self).obj_create(bundle, user=bundle.request.user)\n\n  File \"/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/tastypie/resources.py\", line 2150, in obj_create\n    return self.save(bundle)\n\n  File \"/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/tastypie/resources.py\", line 2301, in save\n    self.save_m2m(m2m_bundle)\n\n  File \"/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/tastypie/resources.py\", line 2432, in save_m2m\n    related_mngr.add(*related_objs)\n\nAttributeError: 'QuerySet' object has no attribute 'add'\n"}

我不知道发生了什么,但反向关系“出租车”是一个大问题。

1 个答案:

答案 0 :(得分:2)

简短回答:

由于出租车场不是真实的场地,我会在LocationResource定义中为你的出租车场添加一个“readonly = false”参数。

此外,您可以简单地删除出租车区域并将其添加到脱水方法

def dehydrate(self, bundle):
     """ LocationResource dehydrate method.

     Adds taxi urls
     """
     taxi_resource = TaxiResource()
     bundle.data['taxi'] = []

     for taxi in Taxi.objects.filter(user=bundle.obj.user):
         taxi_uri = taxi_resource.get_resource_uri(taxi)
         bundle.data['taxi'].append(taxi_uri)

     return bundle

Long asnswer:

问题是你告诉tastypie你的LocationResource有一个名为taxi的ToManyField字段。由于这是一个ModelResource,因此tastypie会将您的Location模型转换为ManyToManyField类型的出租车字段。

在django中,当您在ManyToMany关系中创建新关系时,它按如下方式完成:

location.taxis.add(taxi)

现在,在资源配置中,您放置了以下内容:

taxi = fields.ToManyField(
    TaxiResource, 
    attribute=lambda bundle: Taxi.objects.filter(user=bundle.obj.user),
    full=True, null=True
)

这说明当tastypie尝试创建一个新的Location对象,并且你填写出租车字段时,它会尝试执行以下操作:

Taxi.objects.filter(user=bundle.obj.unser).add(taxi)

显然,Queryset对象没有“add”方法,因为这只是一个查询集而不是ManyToMany关系。