Python with Django pointers – Part 2: Apps, MVC(T) and Models

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:

  1. Use “python manage.py startapp name_of_app”
  2. 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:

  1. In models.py of an app, use following syntax
    class Name(models.Model):
    field = models.TypeField()
  2. Add this to Model Class to display human readable objects in models
    def __str__(self):
    return self.field
  3. To add a model to Django Admin, in admin.py, use following syntax
    from .models import Modelname
    admin.site.register(Modelname)
  4. For date, time and numbers data types (non-string), to allow blank fields, use
    field = models.TypeField(blank=True, null=True)

Having a growth mindset

I've been thinking recently about how I haven't been progressing as much as I would have liked to these past few years. Part of it could simply be attributed to the fact that I've had a pretty eventful few years getting hitched, getting my own place etc. However, a conversation today triggered my memory of how I told myself I would keep growing and keep improving, and I need to make sure I get all the help I can get to do that.

One of the key factors for growth (and I mean the kind of growth that includes the expansion and maturity of your mind and soul) to me, is actually what you constantly feed your mind with. This is one of the reasons I love reading. It expands your thinking and mind, and encourages new ideas, new perspectives. I love it so much, that I quite often hear complaints from the wife that I'm too engrossed in reading, and "neglecting" her (and I assure you I have absolutely no intention of doing so).

The challenge with reading, though, is that once you reach the end of the article / book / text, you are usually stuck with the "what now?" question. For example, block chain is the newest topic that's hot everywhere, and reading up about it is really interesting and insightful, but after you have read and understood it (at least a bit more than before you read it), what do you do now?

To me, I always reflect on how this can impact me. In the case of block chain, the obvious answer to "what now", is "so should I start investing in Bitcoin / Etherium?" Coming to this point is something that I can definitely do fairly consistently, but it's the next step that stumps me quite often.

You see, I have done a lot of introspection, and knowing myself, I always need to talk to someone about these new ideas, and gain further perspective on how / why we can consider pursuing them further. The problem, however, is that you can only have those levels of sharing with someone who has the same level of interests or thinking as you do, and that requires you to have a group of forward thinking, worldview challenging, and matured people around you, consistently interacting and influencing you. It's very true that you should choose who you hang out with because they will either challenge you to grow, or cause you to stagnate in your thinking.

In a way, I seek assurance in any new venture by bouncing my ideas off of others, and it is therefore crucial and essential for me, as a person, to always have people to talk to who can elevate my thinking and perspectives. I need mentors in my life to be able to help me grow in many areas. It is this understanding of myself that leads me to feel fairly frustrated at this point because I feel I don't really have many people like that in my life now, and it is not for the lack of trying.

Just to clarify, I'm not talking about getting someone to tell me step by step what to do, but in any kind of mentoring or discipleship, a lot is imparted simply in the constant interaction because the reinforcement of new ideas happens then. For me personally, I need a certain level of reinforcement to induce an action on my part – again because I understand myself not to be someone who can throw myself into something just like that. A lot of thought needs to go into it first.

So now comes the most difficult part: Actually finding the right people with the mindset and thinking who can, and more importantly, are willing, to build that relationship and connection. It might sound a little selfish, but I'd really like to be able to find someone to encourage me and help me grow. All I can say is that on my part, I think I can also help others grow and sharpen their minds and thinking as well, assuming we can find that common ground.

It would be an investment of time and effort from both sides, but I do believe it is well worth it, if growth is a priority.