5

我在添加与 ApiKey 用户相关的资源时遇到了一些问题,问题正是“出租车”字段,当我评论时,“create_object”工作正常。有资源

资源

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"}

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

4

1 回答 1

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

长答案:

问题是您告诉tastepie 您的LocationResource 有一个名为tax 的ToManyField 字段。由于这是一个 ModelResource,tastepie 将转换为您的 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
)

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

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

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

于 2013-10-04T19:09:42.880 回答