paynestrike
paynestrike

Reputation: 4658

errors when using tastypie resource in view

here is my resource:

class ImageResource(ModelResource):
     album = fields.ForeignKey(AlbumResource, 'album')
     upload_by = fields.ForeignKey(UserResource, 'upload_by')


     class Meta:
         always_return_data=True
         filtering = {
                     "album": ('exact',),
                      }
    queryset = Image.objects.all()
    cache = SimpleCache(timeout=100)
    resource_name = 'image'
    authorization = ImageAuthorization()

class ImageAuthorization(Authorization):
    def read_list(self, object_list, bundle):
         # This assumes a ``QuerySet`` from ``ModelResource``.
         userprofile = UserProfile.objects.get(user = bundle.request.user)
         album = Album.objects.filter(family=userprofile.family)
         return object_list.filter(album__in=album)

and when I try to use ImageResource in view like:

 @csrf_exempt
def upload(request):
    if request.method == 'POST':
       if request.FILES:
            uploadedfile = request.FILES
            file = uploadedfile['item']
            album = Album.objects.get(pk=int(request.POST['album_id']))
            img = Image(
                album = album,
                name = file.name,
                src=file,
                upload_by = request.user,
                )
            # img.save()

            ir = ImageResource()
            uploaded_img = ir.obj_get(src=file)
            print uploaded_img


    return HttpResponse('True')

this will always rasie an error says

obj_get() takes exactly 2 arguments (1 given)

what's wrong with my code??? and how can I get the just uploaded image's resouce

Upvotes: 1

Views: 315

Answers (1)

Hedde van der Heide
Hedde van der Heide

Reputation: 22449

Why are you trying to create instances of ImageResource? That makes no sense.

obj_get is a method for a tastypie resource, which is part of the resource flow chart. It expects a bundle object.

obj_get(self, bundle, **kwargs): ...

You do not have to create a resource on the fly for every image you upload, you don't even need to instantiate one, as the url module does this for you.

I recommend you re-read the documentation and register an ImageResource and/or AlbumResource accordingly. Those resources will pickup uploaded images or albums automatically after you register the resources to your urls module.

Upvotes: 2

Related Questions