Django Apps
A Django project is typically formed from several Apps, each pertaining to a certain function within a website. Usually, I would expect to create one App for say, site updates and announcements, another App for commercial transactions with a shopping cart and checkout functions, yet another App for a forum etc.
Django comes with a built in Admin Site App, allowing trusted site administrators to quickly create, edit, delete and otherwise manage content. This is considered a common function that is required for just about ANY website, and covers things like management and authentication of users, display and management of forms, input validation, and translating all that into SQL statements for updates into the database. Having this by default really saves a lot of hassle when building a website and is a key feature available in the Django Framework.
Overall, since Apps can be made up of other Apps, it is easy to take a top down approach in designing the overall website functions, as breaking them down into Apps (or smaller Apps within an App) would make it much easier to architect the entire system. The team can then focus on individual Apps which will generate the functionalities required.
Key things for Apps:
- Use “python manage.py startapp name_of_app”
- Edit “settings.py” at the INSTALLED_APPS line with
name_of_app.apps.Name_of_appConfig
Every App typically embodies the MVC structure which is what makes Apps so powerful because it is “self-sufficient” and can thus be replicated and utilized in different projects, with some minor tweaks done to the settings.
The MVC Structure
One of the coolest things about frameworks, is the ability to manage data separately from the logical layer, as well as the presentation layer. The MVC structure really makes things easier as you can split the management of all three layers making it cleaner and faster to make changes.
Edit: I previously stopped at describing Django as using the MVC concept above, but would like to correct it by saying that in there is a difference from a typical MVC structure as opposed to Django’s MVT structure. I think this explains the concept a lot better!
Models
Models are the foundation for data that drives your website. Every piece of dynamic data has to be defined in a model and stored in the database. Being able to manage how data is structured is quite typical in a framework, and Django does this very well by providing migration functions. Changes to the database level structure can be affected by using the “check”, “makemigrations” and “migrate” commands. This ensures consistency while developing where you can choose to update a column of a table for example by simply running these commands.
Of course, Django’s models can also have additional attributes which is on top of the database layer, making them extremely flexible.
Recap
So just a few key things for me:
- In models.py of an app, use following syntax
class Name(models.Model):
field = models.TypeField() - Add this to Model Class to display human readable objects in models
def __str__(self):
return self.field - To add a model to Django Admin, in admin.py, use following syntax
from .models import Modelname
admin.site.register(Modelname) - For date, time and numbers data types (non-string), to allow blank fields, use
field = models.TypeField(blank=True, null=True)