Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Is separating views.py into its own module a good idea?

Dilemma

My views.py gets pretty unwieldy, so I want to separate it into a separate views module inside of my app. However, I'm not sure this is a good idea, for two reasons:

  1. If my views file is the same name as the app name, I cannot import the model without using django.db.get_model, therefore I am worried my approach may be flawed. I have heard it is best practice to avoid name collision within modules; should I rename my view files?

  2. I'm not sure if creating a views module is considered good practice within the Django community in general.

Example

For example, for an app named blogs, with a Blog model and a Post model:

blogs/
    __init__.py
    models.py
    urls.py
    views/
        __init__.py
        blogs.py
        posts.py

Here is my blogs.views.blogs:

# project/blogs/views/blogs.py

from django.db.models import get_model
from django.shortcuts import get_object_or_404
from django.views.generic import ListView, DetailView

# Cannot import model directly, results in `ImportError: No module named models`.
# This can be resolved if I resolve the name collision between this file and
# the app itself.
#
# from blogs.models import Blog


class BlogListView(ListView):
    model = get_model('blogs', 'Blog')

    def get_queryset(self):
        return self.model.objects.all()


class BlogDetailView(DetailView):
    model = get_model('blogs', 'Blog')

    def get_object(self):
        blog_pk = self.kwargs.get('blog_pk')
        return get_object_or_404(self.model.objects, pk=blog_pk)

Question

My question is twofold:

  1. Should I be separating my views in the first place?

  2. If so, is using get_model a good idea, or is there a way I can import my model directly without using this method? Or should I change my view file names, for example by adding the suffix _views (e.g.: blogs.views.blogs_views.py) in order to avoid the problem altogether?

like image 926
modocache Avatar asked May 03 '12 11:05

modocache


2 Answers

I cannot import the model without using django.db.get_model

You can: from project_name.app_name.models import MyModel And it's preferable way, 'relative imports for intra-package imports are highly discouraged', - as said in PEP-8.

There shouldn't be any problems with names, views.py has no special meaning in Django, it's just a convention.

You can keep your views in any file in any module under any name you want. So there is no special rules here, if you think that separating the module into submodules will be good, do it.

like image 106
DrTyrsa Avatar answered Sep 22 '22 12:09

DrTyrsa


As DrTyrsa points out, views has no special meaning. So as an alternative to creating a subpackage, you could just create several files at the same level as the existing views.py - blog_views.py, posts_views.py, etc. As long as you use the correct reference in urls.py, this works fine.

like image 40
Daniel Roseman Avatar answered Sep 18 '22 12:09

Daniel Roseman