Django - Where to save forms - django

I like to keep my Django app organized and so I wonder where I should put the code of forms in? Should I write the code in the models file or should I create a new file (i.e. forms.py)?
Does Django have any guidelines for that I should use follow and where do most of the other developer put the forms in?

Forms are usually put inside the forms.py under a django app, just near the models.py. Though, if there are a lot of forms in the forms.py, you can make a package from it and split forms into logical parts.
Also see:
Django Applications best practices from LincolnLoop
An Updated General Django Project (>= 1.5) Structure or Folder layout
Good open source django project for learning
Open-Source Django projects

Related

should I use drf APIView's alongside with django Views in a production project?

I am just learning djangorestframework and my question is: "should I use APIView's alongside with Views?" my friend told me that I should have an api app and for some parts you should only use normal django Views and other parts related to admins the APView.
Now I'm a bit confused what should my project structure look like for exmple in an ecommerce project?
Thanks for your help!

reusable app (package) in Django - how to add extra models

I am writing a small package that extends the django app that is used by many of my colleagues locally. So right now they can simply add it via pip, and then they add this extension in INSTALLED_APPS in settings.py.
But the problem is that I can't add the new models to this extension (or at least I don't figure out yet how to do it correctly) because then the guys who would like to use my extension, have to sync their database or make migrations so their database contains the models needed for extension.
Is it possible (and right thing to do) to add new models to the current django project 'silently' as soon as the user adds the app to INSTALLED_APPS?

django apps and models.py in 1.7 - what exactly has changed?

Django 1.7 has introduced some apparently major changes for how apps work
https://docs.djangoproject.com/en/dev/releases/1.7/#app-loading-refactor
These release notes seem to be saying that you can define models outside of models.py and we don't even need a models.py (or models/__init__.py) inside an app.
Am I misunderstanding this? If not, could someone explain where we define our models if they are not in models.py and how does django find and load them?
You should still define your models in models.py.
Before the app refactor in 1.7 there wasn't a unified API for declaring metadata about your app. In particular, the way Django determined whether something was an app or not was by looking for a models.py file. That was not an elegant system, especially when you consider that some apps don't even have models (for example, the app might just provide management commands).
Now that the AppConfig API exists it's no longer necessary to require the existence of a models.py. However, it's still the natural, and default, place to define your models.
How does django find and load them?
From the documentation: "You must define or import all models in your application’s models.py or models/__init__.py."
That suggests the following practice: if you don't have any models, don't include a models.py. If you have a file's worth of models, put them in models.py. If you have a bunch of models and want to spread them out over multiple files, put the files in a models submodule and import their contents in models/__init__.py.

Does Django have a level of modulation?

The basic Django app contains models and views, however, this structure seems not enough for a complex web app. How can a designer accomplish a certain level of modulation with Django?
A complex web app can consist of many different Django apps. A Django app is basically a module centered around a set of URLs and/or models.
Besides that, it's all just Python. You can split off business logic code to any Python modules you want, put views in any modules you want, and turn models.py into a models/ package with submodules (as long as the package's __init__.py imports * from all the submodules, I think).
A Django project will often consist of many Django apps
When you start to build a "web app" with Django you are creating a project... inside this you will structure your code into one or more Django apps specific to your project. You will likely make use of a number of 3rd party reusable Django apps as well.
See the tutorial:
https://docs.djangoproject.com/en/dev/intro/tutorial01/#creating-a-project

Where is recommended spot for storing admin customizations for Django contrib apps?

I want to add Django Sessions to my Django Admin, and I am following an SO post about this, but it is unclear where I store this code. Do I put it in an admin.py file? Under what directory?
In short, it doesn't matter. You can put the code into any of your apps' admin.py files. However, in situations like these I tend to use a generic app in my project, usually named something like utils, that exists for the sole purpose of housing code that doesn't belong to one specific app or could be used by multiple apps.
If you want to be more specific, you can create a sessions app in your project specifically devoted to this code and any other code related to session management for your project, or perhaps an existing app that is somewhat related. For example, I put customizations to the User admin in my accounts app that holds the UserProfile model.