well I have a doubt, in general you will have many models in a models.py , and that model would have many fields under it(Charfield, foreign key etc...) How many model object are too much in a single model file, for eg if I have 50 model object is it too much? Pls comment your thought on limit for the model object and what's the solution....
The "hard limit" should be very high. But to keep you project structured I'd recommend you to split them into separate apps or model files. There's no recommended number of models per file, but I personally try to stick to max 7 to keep everything organized. You can go higher if you prefer.
You can either structure like this, with separate apps:
my_django_project
manage.py
app1/
models.py
app2/
models.py
app3/
models.py
my_django_project/
settings.py
or like this within the same app:
my_django_project
manage.py
app1/
models/
__init__.py
mymodel1.py
mymodel2.py
mymodel3.py
my_django_project/
settings.py
Inside __init__.py you have to import all models like this:
# __init__.py
from .mymodel1 import *
from .mymodel2 import *
from .mymodel3 import *
Which one you choose is up to you. I prefer to have separate apps. And try to keep the in a logically separated, i.e. which models belongs in the same app?
Related
Issue #1
I'm looking for the best practice in order to declare the additional data in a Django project. Imagine I need to store a huge list of choices to use in app1/models.py (I have seen a lot of best practices keep declaring those choices in the body of the models). In another scenario, Imagine I need to keep tens of config data in order to use them in my views.py. Is it common to declare them in the function/class?
In my opinion, there would be two common methods in order to do that. (Not sure even recommended at all)
Method #1
I can actually keep them all in my settings.py file and access them with importing (like from django.conf import settings).
settings.py:
...
NBA_TEAMS = (
("red", "Bulls"),
("yellow", "Lakers"),
...
)
Method #2
Keep them all in a file named data.py right next to the settings.py, and try importing it at the end of the settings file like this.
settings.py
...
try:
from .data import NBA_TEAMS, ..
except:
pass
Issue #2
Is there any best practice for creating modules in a Django project? Imagine I need to create a data validator function that receives string data from a form submission in views.py. Where should I keep those kinds of functions/validators/generators?
views.py
from --- import validateUsername
def submission(request):
...
form = ContactForm(request.POST)
if validateUsername(form.cleaned_data['username']):
...
Is there any best practice for these issues? Thank you.
As per my suggestions for both your issues you should create one constants.py and one utils.py file.
Now depends on your use case if you want to use the same constants in multiple apps within your project then define it at the root next to manage.py or if the constants are specific to app then create it under the specific app folder next to views.py file.
Same for utils.py you should define all the utility functions / methods in one utils.py file and use it in all the apps within your project.
In Django you name your files like models.py, views.py, urls.py and so on. I wonder, if this naming convention is mandatory for Django. Will Django's functionality break if you place your models in a file called foo.py? I mean, only the import-line should change, right? Or is there any magic with this named files done by the framework?
Of course, I won't give my files shitty names; but I am just curious.
views, urls can be configured.
url: You can defined your one urls by setting <project>.settings.ROOT_URLCONF, and include your apps' urls.
views: Import your views or use view names as you want.
But for models, there's assumption about the model module name in the django code and other third-party apps. (https://github.com/django/django/blob/stable/1.7.x/django/apps/config.py#L9)
The best is to create modules instead for a couple of reasons:
you'll keep it consistent with Django conventions making it easier for others to work on it
you can give more descriptive appropriate names to your files
you will avoid really long files
So you'd have:
- my_application
- urls
- sub_set_urls_1.py
- etc.
- models
__init__.py <= import your models in here
sub_set_models_1.py
etc.
- views
sub_set_views_1.py
etc.
It's a bit more involved for the models, you need to import the models in __init__.py. Another way is to have a models.py file and put your models somewhere else: Split models.py into several files
I'm working on a web application with Django framework. I have this structure :
project
web_app
models
models.py
rwtool
settings.py
urls.py
views.py
I defined my models in models.py file. In my settings file I added this line project.web_app in INSTALLED_APPS. When I did
python mange.py syncdb
the models are not created. To create them I must add the project.web_app.models to my settings file. I have looked to others project and they don't do like this and it works. Is it correct what I'm doing?
models.py should be under your app folder (in your case "web_app").
If you want to separate your models then you have to turn models folder into a python package, in this case add an __init__.py in that folder and make sure that in the init file your models are accessible to the outside world at the path "web_app.models.ModelName".
So, solution 1 (standard Django approach):
project
web_app
models.py
Solution 2:
project
web_app
models
__init__.py
ModelOne.py
ModelTwo.py...
the __init__.py will contains something like:
from .ModelOne import ModelOne
from .ModelTwo import ModelTwo
The approach N2 can be also used for views and other stuff too ;)
First add your app to INSTALLED_APPS , and then run
python manage.py inspectdb > somefile.txt
You can get quickly check out if your database structure is matching your django models.
If python manage.py inspectdb > somefile.txt not created any structure, then make sure that your app is added to to INSTALLED_APPS.
I am using Django wiki module in my project , IN wiki module models are created in their different files.Structure are as follows.
models/
article.py
abcd.py
...
__init.py__
Now I want to add some new models into article.py but after creating models when I try to migrate them I am getting the message , Nothing seems to change. However If I change any field of existing model then migration catch the change.
My init.py files imports all models of articles.py , So I think migration must have created the models but It doesn't . so can anyone tell me where I am wrong.
Thanks
I have started work on a local app for myself that runs through the browser. Having recently gone through the django tutorial I'm thinking that it might be better to use django rather than just plain python.
There's one problem: I have at least 20 models and each will have many functions. Quite simply it's going to create one huge models file and probably huge views too. How do I split them up?
The models are all related so I can't simply make them into separate apps can I?
This is a pretty common need... I can't imagine wading through a models.py file that's 10,000 lines long :-)
You can split up the models.py file (and views.py too) into a pacakge. In this case, your project tree will look like:
/my_proj
/myapp
/models
__init__.py
person.py
The __init__.py file makes the folder into a package. The only gotcha is to be sure to define an inner Meta class for your models that indicate the app_label for the model, otherwise Django will have trouble building your schema:
class Person(models.Model):
name = models.CharField(max_length=128)
class Meta:
app_label = 'myapp'
Once that's done, import the model in your __init__.py file so that Django and sync db will find it:
from person import Person
This way you can still do from myapp.models import Person
"I have at least 20 models" -- this is probably more than one Django "app" and is more like a Django "project" with several small "apps"
I like to partition things around topics or subject areas that have a few (1 to 5) models. This becomes a Django "app" -- and is the useful unit of reusability.
The overall "project" is a collection of apps that presents the integrated thing built up of separate pieces.
This also helps for project management since each "app" can become a sprint with a release at th end.
The models are all related so I cant's
simply make them into separate apps
can I?
You can separate them into separate apps. To use a model in one app from another app you just import it in the same way you would import django.contrib apps.
Having 20 models in one app might be a sign that you should break it up in smaller ones.
The purpose of a Django app is to have a small single-purpose piece of code, that fits nicelly together.
So, if you had a e-commerce site, you might have a shopping_cart app, a billing app, and so on.
Keep in mind that there is really no problem in apps depending on each other (although it's always better if they can be decoupled), but you should not have an app doing two very distinct things.
The article Django tips: laying out an application might help you. As always, take everything you read with a grain of salt (including this answer).
You can break up the models over multiple files. This goes for views as well.
You can split them into separate files and simply have imports at the top of your main models.py field.
Whether you'd really want to is another question.