Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse relation "ToManyField" in tastypie and obj_create error

I'm having some problems adding resources, relationated by ApiKey User, the problem exactly is the "taxi" field, when I comment that, the "create_object" works fine. There is the resource

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)

Models

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)

And the error when I try to create some resources is:

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

I have no clue about what is happening, but the reverse relation "taxi" is the big problem.

like image 916
Rodrigo Amaro Reveco Avatar asked Nov 03 '22 20:11

Rodrigo Amaro Reveco


1 Answers

Short answer:

Since the taxi field is not a real field in location, I would add a "readonly=false" parameter to your taxi field in the LocationResource definition.

Also, you could simply remove the taxi field and add it in the dehydrate method

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:

The problem is that you are telling tastypie that your LocationResource has a ToManyField field called taxi. Since this is a ModelResource, tastypie will translate that your Location model has a taxi field of type ManyToManyField.

In django, when you create a new relationship in a ManyToMany relation, it is done as follows:

location.taxis.add(taxi)

Now, in the Resource configuration you placed the follwing:

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

This states that when tastypie tries to create a new Location object, and you give fill in the taxi field, then it will try to do the following:

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

Clearly the Queryset object does not have an "add" method, since this is simply a queryset and not a ManyToMany relationship.

like image 142
ignacio.munizaga Avatar answered Nov 15 '22 07:11

ignacio.munizaga