Search code examples
pythondjangoresttastypiepython-requests

Tasty-Pie - pulling in related fields, without using full=True?


I have a app that uses Python Requests to query a Tasty-Pie enabled Django app.

I have a model called Application, with a corresponding Tasty-Pie resource.

This model/Resource has several foreign keys that link Application to other models (e.g. Binary, Host, Colocation etc.)

I'm using a Tasty-Pie filter to get a subset of Applications, then I want to print a nice table of Applications, along with some fields from those related models.

Right now, I'm using the following to get a table of Applications:

def get_applications(self, parsed_args):
    r = requests.get('http://foobar.com:8000/api/v1/application/?name__iregex={0}&format=json'.format(parsed_args.applications))
    print(r.url)
    return r  
def application_iter(self, parsed_args):
    for application in self.get_applications(parsed_args).json['objects']:
        yield (application['name'], application['author'], application['some_other_field'])
def take_action(self, parsed_args):
    return(('Name', 'Author', 'Some Other Field),
            self.application_iter_iter(parsed_args),
        )

My question is, what is the "recommended", or idiomatic way of pulling in all the related fields? Is there a way to extend the above to do this?

I get the impression that full=True is a bad practice, and that using resource URI's is a better way.

How can I do this whilst minimising the number of requests and DB hits?

Cheers, Victor


Solution

  • why do you think that full=True is bad?

    https://django-tastypie.readthedocs.org/en/latest/resources.html#why-resource-uris

    Ideology aside, you should use whatever suits you. If you prefer fewer requests & fewer endpoints, use of full=True is available, but be aware of the consequences of each approach.

    You can do whatewer you like if it can be read cleanly and if it does what you want. "full=True" is there to be used by developers