GithubHelp home page GithubHelp logo

dan-klasson / django-tastypie-specified-fields Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 2.0 178 KB

Only fetching and outputting fields that are user specified

License: Do What The F*ck You Want To Public License

Python 100.00%

django-tastypie-specified-fields's Introduction

django-tastypie-specified-fields

With this extension Django will only fetch and return the fields you specify. This is useful when you have a lot of fields in your model and/or you have a lot of traffic.

For example, you can do the following:

curl -H "Accept: application/json" http://127.0.0.1:8000/api/v1/book/?fields=title,publication_date,publisher__name,publisher__country__name

Which produces something like:

"objects": [
    {
        "publication_date": "1983-07-04",
        "publisher__country__name": "U.S",
        "publisher__name": "Viking Press",
        "resource_uri": "/api/v1/book/1/",
        "title": "Misery"
    },
    {
        "publication_date": "1945-08-17",
        "publisher__country__name": "U.K",
        "publisher__name": "Penguin Press",
        "resource_uri": "/api/v1/book/2/",
        "title": "Animal Farm"
    },
    {
        "publication_date": "1949-06-08",
        "publisher__country__name": "U.K",
        "publisher__name": "Penguin Press",
        "resource_uri": "/api/v1/book/3/",
        "title": "1984"
    }
]

And yields the following two queries:

SELECT COUNT(*) FROM "app_book"

SELECT "app_book"."id", "app_book"."title", "app_book"."publisher_id", "app_book"."publication_date", "app_publisher"."id", "app_publisher"."name", "app_publisher"."country_id", "app_country"."id", "app_country"."name" FROM "app_book" INNER JOIN "app_publisher" ON ("app_book"."publisher_id" = "app_publisher"."id") INNER JOIN "app_country" ON ("app_publisher"."country_id" = "app_country"."id") LIMIT 20

You can also filter on m2m relations:

curl -H "Accept: application/json" http://127.0.0.1:8000/api/v1/book/?fields=title,publication_date,publisher__name&author=1

Demo

There is an example project included you can play around with. The credentials are demo/demo.

TODO

  • Return the resource_uri when user specifies a related field without sub items
  • Make it support m2m fields
  • Create the ability to specify columns for each API in the Admin

django-tastypie-specified-fields's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

django-tastypie-specified-fields's Issues

Installation

Not able to install to my django project.
pip install django-tastypie-specified-fields==0.1
returns No matching distribution found for django-tastypie-specified-fields==0.1

Fields is not part of the cache key

I didn't have the opportunity to go very far on this, but I suspect the cache is not usable.
I think "fields" query string parameter is not part of the key - so the first call will be cached and returned no matter which fields are specified (if any) on subsequent requests. I'll dig some more when i have some spare time and maybe suggest a patch.

self.specified_fields not thread-safe

self.specified_fields can be shared across threads. This can produce strange results, when you issue multiple requests in quick succession with different specified fields. I don't think a resource should be modified after initialization, since it's initialized once per process.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.