I have a flask project which allows users to create projects and to upload files to those projects. The URL structure looks something like /projects/<project_id>/documents/<document_id>.
My problem was that I was repeating /projects/ and /documents/ a lot for all the views that handled projects and documents (a view to list, a view to show, etc. etc.) so I thought that I could make life easier with blueprints. I decided that I could organize my code something like this:
app
projects
views.py
forms.py
__init__.py
documents
views.py
forms.py
__init__.py
views.py
__init__.py
run.py
However, there are forms (as well as pluggable views) that are used in both documents and projects.
My question is, how can I have code in common between my two blueprints? It's also possible that there's a way easier way to implement this (since all I really need is to avoid redundancy in typing out the URL), and I'd love to hear one.
Since you can have multiple documents per project and assuming that one document cannot be shared across multiple projects, I would suggest you only create a "Project" Blueprint and put the "Document" under it as a package. Something like:
app/
projects/
views.py
forms.py
__init__.py
documents/
views.py
forms.py
__init__.py
views.py
__init__.py
run.py
Now, under Projects/forms.py, you can add all the common code that is also relevant to Documents. Additionally, documents have their own forms.py but that could be very specific only to a document.
Related
We've been developing an application using django and django rest framework for a while now and we're reaching a point where our viewsets.py and serializers.py files are getting too large.
Our current structure is very similar to what many posts describe (eg. Large Django application layout) but they only seem to suggest creating new applications as the best route to maintaining manageable directories.
Our current set up is as follows:
project
app1
models.py
serializers.py
viewsets.py
...
app2
models.py
serializers.py
viewsets.py
...
For our use case, our models.py are small and discrete enough that I don't think we need to separate them out into separate applications, but I'm not sure what else we can do.
It's not really clear what you're asking here. Neither Django nor Django Rest Framework accord any special status to the file serializers.py; like views.py, it's just a place where you store things that are imported by other functions. So it's absolutely fine to split that file up in any way you like.
For instance, you could have a serializers package, ie a directory with many Python files in it (including a possibly-empty __init__.py); you could simply have variously named files in the app directory with different serializer classes in them; and so on.
Either split your app into sub application based on usage as mention above.
are you can split it based database like,
app->
tables->
models.py
viewset.py
serializer.py
views->
models.py
viewset.py
serializer.py
procedures->
models.py
viewset.py
serializer.py
I am a a beginner learning django rest framework. I want to know if it is a good idea to create all my classes in one model file (model.py) of my app. Is it a good idea?
I generally start off having all of the models in a single models.py file. As the project gets larger and the app more complicated I'll sometimes end up breaking it out into multiple files (generally in a models directory).
An example of this is our user's app. We have multiple sets of users with different permissions in our website. Admin Users, Tour leaders, Suppliers, etc... Each one of these have been broken up into its own file to keep the logic contained.
users/
models/
__init__.py
base.py // Stores the abstract User model used across all Users
leaders.py // Stores specific models four Tour Leaders
suppliers.py // Stores specific models for Suppliers
money.py // Stores models related to how we pay Tour Leaders (lots of logic here)
views.py
forms.py
urls.py
This allows us to contain all of the business logic into specific files, making it easier for developers to find what they want.
In our __init__.py file we import all of the models.
from users.models.base import *
from users.models.leaders import *
from users.models.suppliers import *
from users.models.money import *
You can, but if you have quite a few models it's better to create a folder called models/ inside your app, and put your models in there. You will also need to make a __init__.py file to import your models into for migrations to work (Django only expects to import app.models, and the __init__.py file handles that).
So your folder will look like:
models/
__init__.py
modelA.py
modelB.py
modelC.py
And your __init__.py will look like:
from .modelA import <modelName>
from .modelB import <modelName>
from .modelC import <modelName>
This question makes in part reference to Django directory structure? .
How should I place the models.py in Django?
Project_name/
Application1/
models.py
Application2/
models.py
or
Project_name/
DB/
models.py
Which one should I use? why?
Daniel Roseman is correct, when running manage.py startapp appname, Django will automatically create a directory structure with a separate models.py per app like:
appname/
models.py
As well as being convention (if other Django developers come to look at your code, this is where they will expect to find your models) it also allows people to create reusable apps
e.g. by simply copying the appname directory I have all the URL patterns (if any), views, models, forms, tests and everything else, rather than having to pull out all the files from a large separate directory.
The first option is correct in Django each app has her own models.py or models directory.
I'm now working with template tags and one thing I haven't understood is why do template-tags have to be stored inside a templatetags directory in the app. Is there an underlying reason for this? Is it possible to store them in a templatetags.py file somehow so that I can reduce the extra bloat around having extra directories?
Django expects you to arrange your apps in certain ways. Requiring a templatetags directory in the app is not the exception. Some other requirements are:
ModelAdmin in admin.py (for autodiscovery)
Models in models.py
Management commands in management/commands directory
Fixtures in the fixtures directory
Unlike templates, where there are hooks to specify how your templates are loaded, there is no easy way to store your templatetags modules in a different location.
While I agree with the rationale of Alasdair, that the standard django expectation is to have admin, models, templates and templatetags all within the respective app folders, I think there is unnecessary boilerplate associated.
I think it is for that reason, that there are plenty of 3rd party apps: https://github.com/ojii/django-classy-tags, https://github.com/justquick/django-native-tags and https://github.com/alex/django-templatetag-sugar to reduce the boiler plate.
I have multiple Django apps, and I have some code in one of my models.py that really applies to all of the apps. Is there a way to move it somewhere generic, outside of a specific app folder?
Examples of non app specific code I have:
def update_groups(sender, user=None, ldap_user=None, **kwargs):
...
django_auth_ldap.backend.populate_user.connect(update_groups)
A function to correctly identify users, and connect to the correct signal.
I also have a model proxy of django.contrib.admin.models.LogEntry and a modelAdmin of that model proxy so users in the admin site can view the change history. But these really don't belong in any one app's models.py.
Well, just create a python module somewhere in your project and then refference it in your models. To do this, you need a directory with __init__.py file:
helpers/
__init__.py
functions.py
Put your code into the functions.py and in any other place you will be able to:
from helpers.functions import update_groups
post_save.connect(update_groups)
Name of the module is up to you, ofcourse.
You could create a template app that could be used or extended (class hierarchy) by all your other apps that could use that code.
__init__.py in my project's directory seems to be a good place to put this stuff. It gets run right away when the server starts so it's available to everything else. It seems to work fine so far.