Pergunta

What i'm trying to do is to add a query result from a model to a modelresource, as you can see in this block of code:

def dehydrate(self, bundle):
    bundle.data['image'] = place_image.image.get(place=1).get(cardinality=0)

I want to add a field to PlaceResource that will contain the image from place_site model where place=1 and cardinality=0. But im recieving an error:

The 'image' attribute can only be accessed from place_image instances

So, my question is: Is it impossible to use the query result from another model in a tastypie modelresource? Im sorry for my bad english, please correct me if something's wrong. Thanks for your time. There's the complete code:

MODELS.py:

class place(models.Model):
    idPlace = models.AutoField(primary_key=True)
    Name = models.CharField(max_length=70)


class place_image(models.Model):
    idImage = models.AutoField(primary_key=True)
    place = models.ForeignKey(place,
                              to_field='idPlace')
    image = ThumbnailerImageField(upload_to="place_images/", blank=True)
    cardinality = models.IntegerField()

API.py

from models import place
from models import place_image


class PlaceResource(ModelResource):

    class Meta:
        queryset = place.objects.all()
        resource_name = 'place'
        filtering = {"name": ALL}
        allowed_methods = ['get']


    def dehydrate(self, bundle):
        bundle.data['image'] = place_image.image.get(place=1).get(cardinality=0)

        return bundle


class PlaceImageResource(ModelResource):
    place = fields.ForeignKey(PlaceResource, 'place')

    class Meta:
        queryset = place_image.objects.all()
        resource_name = 'placeimage'
        filtering = {"place": ALL_WITH_RELATIONS}
        allowed_methods = ['get']
Foi útil?

Solução

The error you are getting is caused by the fact that you are accessing the image attribute of a model class, not instance.

The object that is being dehydrated in the dehydrate method is stored in obj attribute of the bundle parameter. Also, you are trying to filter place_image models to only those with place=1 and cardinality=0 by accessing the image attribute of place_image model class. Such filtering won't work as image is not a ModelManager instance. You should use objects attribute instead. Furthermore, get() method returns an actual model instance thus a subsequent call to get() will raise AtributeError as your place_image model instances have no attribute get.

So, all in all, your dehydrate should look like this:

def dehydrate(self, bundle):
    bundle.data['image'] = place_image.objects.get(place_id=1, cardinality=0).image
    return bundle

Notice that this code requires the place_image with desired values to exist, otherwise a place_image.DoesNotExist will be thrown.

There is also some redundancy in your models:

  • idPlace and idImage can be removed, as django by default creates an AutoField that is a primary key called id when no other primary key fields are defined
  • place_image.place field has a redundant to_field parameter, as by default ForeignKey points to a primary key field
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top