Structuring an API in a large project

70 views
Skip to first unread message

Cezar Jenkins

unread,
May 1, 2014, 11:59:25 AM5/1/14
to django...@googlegroups.com
Right now I have a large project with an equally large API (done using django rest framework). The current structure is something like this:

api
|-----urls.py
|-----models.py
|
----v1
    |-----views.py
    |-----serializers.py
    |-----permissions.py
    |-----tests.py

etc

As you can guess, the views.py file is pretty big and I want to refactor this out. Currently I have a few options in front of me, the one I'm leaning towards is to put an 'api/v1' package into each app and use the api app to tie all the urls together and hold views that don't fall into an app.

Does anyone have any experience with this and could provide guidance?

Venkatraman S

unread,
May 1, 2014, 12:10:34 PM5/1/14
to django...@googlegroups.com
On Thu, May 1, 2014 at 9:29 PM, Cezar Jenkins <empero...@gmail.com> wrote:
Right now I have a large project with an equally large API (done using django rest framework). The current structure is something like this:

api
|-----urls.py
|-----models.py
|
----v1
    |-----views.py
    |-----serializers.py
    |-----permissions.py
    |-----tests.py

etc

As you can guess, the views.py file is pretty big and I want to refactor this out. Currently I have a few options in front of me, the one I'm leaning towards is to put an 'api/v1' package into each app and use the api app to tie all the urls together and hold views that don't fall into an app.

If the only pain point is the big views file, then why not just refactor that into multiple files under 'v1'  itself - nothing is stopping you from doing that. 

Adam "Cezar" Jenkins

unread,
May 1, 2014, 12:16:33 PM5/1/14
to django...@googlegroups.com
That was one way I started refactoring, but ran into issues. Say I have a destinations.py view file in the api, but I also have a destinations app. In my views file I can't do 'from destinations.models import Destination' cause the name conflicts. So I started having to name things 'destinations_api.py' which seemed really dirty.
 

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAN7tdFT0BAG5XoKoiavKr7v24DqSQTc7D%2Bq5EY7Wgoh_VzvzeA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Venkatraman S

unread,
May 1, 2014, 1:55:24 PM5/1/14
to django...@googlegroups.com
On Thu, May 1, 2014 at 9:46 PM, Adam "Cezar" Jenkins <empero...@gmail.com> wrote:
On Thu, May 1, 2014 at 11:10 AM, Venkatraman S <venk...@gmail.com> wrote:


On Thu, May 1, 2014 at 9:29 PM, Cezar Jenkins <empero...@gmail.com> wrote:
Right now I have a large project with an equally large API (done using django rest framework). The current structure is something like this:

api
|-----urls.py
|-----models.py
|
----v1
    |-----views.py
    |-----serializers.py
    |-----permissions.py
    |-----tests.py

etc

As you can guess, the views.py file is pretty big and I want to refactor this out. Currently I have a few options in front of me, the one I'm leaning towards is to put an 'api/v1' package into each app and use the api app to tie all the urls together and hold views that don't fall into an app.

If the only pain point is the big views file, then why not just refactor that into multiple files under 'v1'  itself - nothing is stopping you from doing that. 

That was one way I started refactoring, but ran into issues. Say I have a destinations.py view file in the api, but I also have a destinations app. In my views file I can't do 'from destinations.models import Destination' cause the name conflicts. So I started having to name things 'destinations_api.py' which seemed really dirty.

When I have to split views.py into multiple files, i generally suffix it with _views; so, in your case it would be destination_views.py .

-V

Mike Dewhirst

unread,
May 1, 2014, 6:36:00 PM5/1/14
to django...@googlegroups.com
On 2/05/2014 2:16 AM, Adam "Cezar" Jenkins wrote:
> On Thu, May 1, 2014 at 11:10 AM, Venkatraman S <venk...@gmail.com
> <mailto:venk...@gmail.com>> wrote:
>
>
>
> On Thu, May 1, 2014 at 9:29 PM, Cezar Jenkins
> <empero...@gmail.com <mailto:empero...@gmail.com>> wrote:
>
> Right now I have a large project with an equally large API (done
> using django rest framework). The current structure is something
> like this:
>
> api
> |-----urls.py
> |-----models.py
> |
> ----v1
> Â Â |-----views.py
> Â Â |-----serializers.py
> Â Â |-----permissions.py
> Â Â |-----tests.py
>
> etc
>
> As you can guess, the views.py file is pretty big and I want to
> refactor this out. Currently I have a few options in front of
> me, the one I'm leaning towards is to put an 'api/v1' package
> into each app and use the api app to tie all the urls together
> and hold views that don't fall into an app.
>
>
> If the only pain point is the big views file, then why not just
> refactor that into multiple files under 'v1' Â itself - nothing is
> stopping you from doing that.Â
>
>
> That was one way I started refactoring, but ran into issues. Say I have
> a destinations.py view file in the api, but I also have a destinations
> app. In my views file I can't do 'from destinations.models import
> Destination' cause the name conflicts. So I started having to name
> things 'destinations_api.py' which seemed really dirty.

I might have missed something here but the usual Django way of
refactoring a large views (models too) file is to convert it into a
directory containing a file for each view. Then you can still refer to
each view using the dotted names as previously. No need to change other
code in other parts of the app which calls them.

> Â
>
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/CAN7tdFT0BAG5XoKoiavKr7v24DqSQTc7D%2Bq5EY7Wgoh_VzvzeA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAN6vQqtUML2-o3pkx02o%2BnsZ0nnYCUC6XULjw--MdRwRyOcsdg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAN6vQqtUML2-o3pkx02o%2BnsZ0nnYCUC6XULjw--MdRwRyOcsdg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Adam "Cezar" Jenkins

unread,
May 1, 2014, 8:29:45 PM5/1/14
to django...@googlegroups.com
You're correct that code outside of those view wouldn't have to change, but the name of the file the view is in can clash with something outside that file.
 

Â

    --
    You received this message because you are subscribed to a topic in
    the Google Groups "Django users" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to

    To post to this group, send email to django...@googlegroups.com
    <https://groups.google.com/d/msgid/django-users/CAN7tdFT0BAG5XoKoiavKr7v24DqSQTc7D%2Bq5EY7Wgoh_VzvzeA%40mail.gmail.com?utm_medium=email&utm_source=footer>.


    For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send

To post to this group, send email to django...@googlegroups.com
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Mike Dewhirst

unread,
May 1, 2014, 9:15:51 PM5/1/14
to django...@googlegroups.com
On 2/05/2014 10:29 AM, Adam "Cezar" Jenkins wrote:
>
>
>
> On Thu, May 1, 2014 at 5:36 PM, Mike Dewhirst <mi...@dewhirst.com.au
> <mailto:mi...@dewhirst.com.au>> wrote:
>
> On 2/05/2014 2:16 AM, Adam "Cezar" Jenkins wrote:
>
> On Thu, May 1, 2014 at 11:10 AM, Venkatraman S
> <venk...@gmail.com <mailto:venk...@gmail.com>
> <mailto:venk...@gmail.com <mailto:venk...@gmail.com>>> wrote:
>
>
>
> Â Â On Thu, May 1, 2014 at 9:29 PM, Cezar Jenkins
> Â Â <empero...@gmail.com <mailto:empero...@gmail.com>
> <mailto:empero...@gmail.com
> <mailto:empero...@gmail.com>__>> wrote:
>
> Â Â Â Â Right now I have a large project with an equally
> large API (done
> Â Â Â Â using django rest framework). The current structure
> is something
> Â Â Â Â like this:
>
> Â Â Â Â api
> Â Â Â Â |-----urls.py
> Â Â Â Â |-----models.py
> Â Â Â Â |
> Â Â Â Â ----v1
>     Â  Â  |-----views.py
>     Â  Â  |-----serializers.py
>     Â  Â  |-----permissions.py
>     Â  Â  |-----tests.py
>
>
> Â Â Â Â etc
>
> Â Â Â Â As you can guess, the views.py file is pretty big
> and I want to
> Â Â Â Â refactor this out. Currently I have a few options in
> front of
> Â Â Â Â me, the one I'm leaning towards is to put an
> 'api/v1' package
> Â Â Â Â into each app and use the api app to tie all the
> urls together
> Â Â Â Â and hold views that don't fall into an app.
>
>
> Â Â If the only pain point is the big views file, then why not
> just
>   refactor that into multiple files under 'v1' Â itself -
> nothing is
>   stopping you from doing that.Â
>
>
>
> That was one way I started refactoring, but ran into issues. Say
> I have
> a destinations.py view file in the api, but I also have a
> destinations
> app. In my views file I can't do 'from destinations.models import
> Destination' cause the name conflicts. So I started having to name
> things 'destinations_api.py' which seemed really dirty.
>
>
> I might have missed something here but the usual Django way of
> refactoring a large views (models too) file is to convert it into a
> directory containing a file for each view. Then you can still refer
> to each view using the dotted names as previously. No need to change
> other code in other parts of the app which calls them.
>
>
> You're correct that code outside of those view wouldn't have to change,
> but the name of the file the view is in can clash with something outside
> that file.

But if inside the app.views.__init__.py file you import the view classes
and defs you can call them without mentioning the filenames within which
they exist eg.

from __future__ import absolute_import (if you are using Python 2.x)
from .conflictingfilename import ClassyView, defined_function

Thereafter, elsewhere you can call ...

app.views.ClassyView.function_within_classyview()
app.views.defined_function()



> Â
>
>
> Â
>
> Â Â --
> Â Â You received this message because you are subscribed to a
> topic in
> Â Â the Google Groups "Django users" group.
> Â Â To unsubscribe from this topic, visit
> Â Â
> https://groups.google.com/d/__topic/django-users/__rizwdsi5o1k/unsubscribe
> <https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe>.
> Â Â To unsubscribe from this group and all its topics, send an
> email to
> Â Â django-users+unsubscribe@__googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>
> Â Â <mailto:django-users+...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
>
> Â Â To post to this group, send email to
> django...@googlegroups.com <mailto:django...@googlegroups.com>
> Â Â <mailto:django-users@__googlegroups.com
> <mailto:django...@googlegroups.com>>.
>
> Â Â Visit this group at
> http://groups.google.com/__group/django-users
> <http://groups.google.com/group/django-users>.
> Â Â To view this discussion on the web visit
> Â Â
> https://groups.google.com/d/__msgid/django-users/__CAN7tdFT0BAG5XoKoiavKr7v24DqSQ__Tc7D%2Bq5EY7Wgoh_VzvzeA%__40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAN7tdFT0BAG5XoKoiavKr7v24DqSQTc7D%2Bq5EY7Wgoh_VzvzeA%40mail.gmail.com>
> Â Â
> <https://groups.google.com/d/__msgid/django-users/__CAN7tdFT0BAG5XoKoiavKr7v24DqSQ__Tc7D%2Bq5EY7Wgoh_VzvzeA%__40mail.gmail.com?utm_medium=__email&utm_source=footer
> <https://groups.google.com/d/msgid/django-users/CAN7tdFT0BAG5XoKoiavKr7v24DqSQTc7D%2Bq5EY7Wgoh_VzvzeA%40mail.gmail.com?utm_medium=email&utm_source=footer>>.
>
>
> Â Â For more options, visit
> https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from
> it, send
> an email to django-users+unsubscribe@__googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>
> <mailto:django-users+...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
>
> To post to this group, send email to
> django...@googlegroups.com <mailto:django...@googlegroups.com>
> <mailto:django-users@__googlegroups.com
> <mailto:django...@googlegroups.com>>.
>
> Visit this group at
> http://groups.google.com/__group/django-users
> <http://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/__msgid/django-users/__CAN6vQqtUML2-o3pkx02o%__2BnsZ0nnYCUC6XULjw--__MdRwRyOcsdg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAN6vQqtUML2-o3pkx02o%2BnsZ0nnYCUC6XULjw--MdRwRyOcsdg%40mail.gmail.com>
> <https://groups.google.com/d/__msgid/django-users/__CAN6vQqtUML2-o3pkx02o%__2BnsZ0nnYCUC6XULjw--__MdRwRyOcsdg%40mail.gmail.com?__utm_medium=email&utm_source=__footer
> <https://groups.google.com/d/msgid/django-users/CAN6vQqtUML2-o3pkx02o%2BnsZ0nnYCUC6XULjw--MdRwRyOcsdg%40mail.gmail.com?utm_medium=email&utm_source=footer>>.
>
> For more options, visit https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/__topic/django-users/__rizwdsi5o1k/unsubscribe
> <https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to
> django-users+unsubscribe@__googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/__group/django-users
> <http://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/__msgid/django-users/5362CC50.__5060905%40dewhirst.com.au
> <https://groups.google.com/d/msgid/django-users/5362CC50.5060905%40dewhirst.com.au>.
>
> For more options, visit https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAN6vQquB1Lsmq%2BeH1D9imZFOVr7H8Gr0%3D5VqaxCE6K_iAB1SYQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAN6vQquB1Lsmq%2BeH1D9imZFOVr7H8Gr0%3D5VqaxCE6K_iAB1SYQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Adam "Cezar" Jenkins

unread,
May 2, 2014, 9:42:44 AM5/2/14
to django...@googlegroups.com
Thanks. This is actually a really great work around.
 



Â


        Â

        Â  Â  --
        Â  Â  You received this message because you are subscribed to a
        topic in
        Â  Â  the Google Groups "Django users" group.
        Â  Â  To unsubscribe from this topic, visit
        Â  Â
        https://groups.google.com/d/__topic/django-users/__rizwdsi5o1k/unsubscribe
        <https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe>.
        Â  Â  To unsubscribe from this group and all its topics, send an
        email to
        Â  Â django-users+unsubscribe@__googlegroups.com
        <mailto:django-users%2Bunsu...@googlegroups.com>
        Â  Â  <mailto:django-users+__unsub...@googlegroups.com

        <mailto:django-users%2Bunsu...@googlegroups.com>>.

        Â  Â  To post to this group, send email to
        django...@googlegroups.com <mailto:django-users@googlegroups.com>
        Â  Â  <mailto:django-users@__googlegroups.com
        <mailto:django-users@googlegroups.com>>.


        Â  Â  Visit this group at
        http://groups.google.com/__group/django-users
        <http://groups.google.com/group/django-users>.
        Â  Â  To view this discussion on the web visit
        Â  Â
        https://groups.google.com/d/__msgid/django-users/__CAN7tdFT0BAG5XoKoiavKr7v24DqSQ__Tc7D%2Bq5EY7Wgoh_VzvzeA%__40mail.gmail.com
        <https://groups.google.com/d/msgid/django-users/CAN7tdFT0BAG5XoKoiavKr7v24DqSQTc7D%2Bq5EY7Wgoh_VzvzeA%40mail.gmail.com>
        Â  Â
        <https://groups.google.com/d/__msgid/django-users/__CAN7tdFT0BAG5XoKoiavKr7v24DqSQ__Tc7D%2Bq5EY7Wgoh_VzvzeA%__40mail.gmail.com?utm_medium=__email&utm_source=footer
        <https://groups.google.com/d/msgid/django-users/CAN7tdFT0BAG5XoKoiavKr7v24DqSQTc7D%2Bq5EY7Wgoh_VzvzeA%40mail.gmail.com?utm_medium=email&utm_source=footer>>.


        Â  Â  For more options, visit
        https://groups.google.com/d/__optout
        <https://groups.google.com/d/optout>.



        --
        You received this message because you are subscribed to the Google
        Groups "Django users" group.
        To unsubscribe from this group and stop receiving emails from
        it, send
        an email to django-users+unsubscribe@__googlegroups.com
        <mailto:django-users%2Bunsu...@googlegroups.com>


        To post to this group, send email to
        <mailto:django-users@__googlegroups.com

        <mailto:django-users@googlegroups.com>>.

        Visit this group at

    To post to this group, send email to django...@googlegroups.com



--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send

To post to this group, send email to django...@googlegroups.com

Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Scot Hacker

unread,
May 2, 2014, 11:44:24 AM5/2/14
to django...@googlegroups.com
On Thursday, May 1, 2014 8:59:25 AM UTC-7, Cezar Jenkins wrote:

As you can guess, the views.py file is pretty big and I want to refactor this out. Currently I have a few options in front of me, the one I'm leaning towards is to put an 'api/v1' package into each app and use the api app to tie all the urls together and hold views that don't fall into an app.



Hmm, I think the convention is to keep api code with each of your apps, so there shouldn't be a danger of anything getting too big / unwieldy:

apps
-- app1
----models.py
----views.py
----apiviews.py
----serializers.py
-- app2
----models.py
----views.py
----apiviews.py
----serializers.py

etc. import statements throughout the project are nice and readable. Works well for our (very large) project anyway.

./s

Adam "Cezar" Jenkins

unread,
May 2, 2014, 1:47:34 PM5/2/14
to django...@googlegroups.com
Thanks. I really like this. 

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/rizwdsi5o1k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages