1

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

paynestrike
  • 4,348
  • 14
  • 46
  • 70

1 Answers1

2

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.

Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • I think you misunderstood, tastypie api does't support uploading file,so I have to use normal view function, and I also use backbone as my frontend framework,so after the image is uploaded,I need to render a thumb view according to the upload feed back, I render the other thumb view use tastypie api feed back, so I figure it should be easy to use tastypie resource in my view to give the same feed back as the other images, if I try to do that on my own using normal view function,I don't think I can generate the same feed back for the uploaded images. Does it make sense? – paynestrike May 28 '13 at 00:54
  • and of course I registed the resource, I render the normal backbone view with the api's feed back – paynestrike May 28 '13 at 00:57
  • 2
    How does it not support uploading files? [example1](http://stackoverflow.com/questions/12381016/django-tastypie-any-example-on-file-upload-in-post), [example2](http://stackoverflow.com/questions/14119031/how-do-you-upload-a-file-with-a-post-request-on-django-tastypie), [example3](https://groups.google.com/forum/?fromgroups#!topic/django-tastypie/2blsUptBbm0), besides the examples you could always return a serialized (json) response? – Hedde van der Heide May 28 '13 at 07:32
  • I did try to use example1 , but I get this error "Sorry, not implemented yet. Please append "?format=json" to your URL.", note I use https://github.com/weixiyen/jquery-filedrop to upload – paynestrike May 28 '13 at 08:00
  • upload file in tastypie is such a pain, it wont work, I tried everything – paynestrike May 28 '13 at 08:47
  • I tried example2, and I get "InMemoryUploadedFile' object has no attribute 'items'" – paynestrike May 28 '13 at 08:53