Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:12:09+00:00 2026-06-02T07:12:09+00:00

I have to implement a new ModelResource that overrides the original obj_get method. What

  • 0

I have to implement a new ModelResource that overrides the original obj_get method. What kind of object do I have to return? An instance of a Django model? It’s not explained well in the Tastypie documentation. Let’s say I have a Django model I want to return turn into json and send back to whoever did the GET request. How is it normally implemented?

I have this code:

  def obj_get(self, request=None, **kwargs):
    return Item.objects.get(id=kwargs['pk'])

It’s just to show you what I am trying to do. I have figured out that it’s not a Django instance that I have to return. What do I have to return?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:12:10+00:00Added an answer on June 2, 2026 at 7:12 am

    Actually that’s exactly what you should return. I recommend that you take a look at Tastypie’s implementation of obj_get:

     def obj_get(self, request=None, **kwargs):
        """
        A ORM-specific implementation of ``obj_get``.
    
        Takes optional ``kwargs``, which are used to narrow the query to find
        the instance.
        """
        try:
            base_object_list = self.get_object_list(request).filter(**kwargs)
            object_list = self.apply_authorization_limits(request, base_object_list)
            stringified_kwargs = ', '.join(["%s=%s" % (k, v) for k, v in kwargs.items()])
    
            if len(object_list) <= 0:
                raise self._meta.object_class.DoesNotExist("Couldn't find an instance of '%s' which matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs))
            elif len(object_list) > 1:
                raise MultipleObjectsReturned("More than '%s' matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs))
    
            return object_list[0]
        except ValueError:
            raise NotFound("Invalid resource lookup data provided (mismatched type).")
    

    As you can see they use self.get_object_list to obtain a list of items meeting the criteria (in this case kwargs should contain pk) and are hoping to get just one item. In such case they return the first (and only item on that list). Otherwise an exception is raised. The list in case of Django is simply a queryset though.

    In general – obj_get should return an object which has properties corresponding to Resource attributes. Good example of this is given in Using Riak for MessageResource where a dictionary is wrapped in RiakObject class so that instead of obj[ 'attribute' ] you can do obj.attribute which is required by Tastypie (and hence Django model instance will work).

    So to summarize, you can return a Django model instance, or if you’re feeling desire for some extra work and would like to worsen the performance, you could build dictionaries out of model instance attributes and wrap them in class like RiakObject mentioned above. I don’t recommend the latter though in case of Django 🙂

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to implement a factory method pattern in C++. The class (C) that
I have to implement a one linked list but it should put object in
Here I am new to Silverlight and I have to implement a video player
I have a studentadministration application. Now I have to implement a new part of
I'm new to Blackberry Development and i have to implement a search functionality. It
Can someone (hopefully Rob) explain how to implement the new SubSonicRepository<> Pattern? I have
Recently I have started to implement new experimental feature for my project. Unfortunately I
I am new to Mongo DB I have to implement it in java. I
I have to implement a new feature in my existing JSF application. As it
I have the following algorithm to implement new in C void *newinc(unsigned int s)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.