我在添加与 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"}
我不知道发生了什么,但反向关系“出租车”是个大问题。