Creating models in Django

- Andrés Cruz

En español
Creating models in Django

In this post we are going to see how we can create a model in Django; in previous posts we already discussed how we can create the connection to the database of a project in Django with MySQL, now we are going to take advantage of that connection to be able to make more interesting views and templates and not just a hello world or a text print or static visas as we saw previously.

This entry provides a fairly general usage for django-models; in such a way, that you will know what they are for and what else we can use them for, for example, for migrations and tables in the database through Django.

What is a model?

Let's remember that a model is simply one of the layers of our MVC, (MTV in Django) with which we work with data, queries, searches, insertions and any operation to the database in general on a table.

Therefore, it is the mechanism that we have to connect to our database and perform any type of operation on it.

Models and the single application file in Django

To create a model, we obviously need to have a Django project and app, which we did earlier, in the app called:

firstProject

We have a file called models.py that is completely empty; as you can deduce from the name of the file, it is used to store or define ALL the models of this application, therefore if you come from PHP like me, this may shock you a bit since in popular frameworks like CodeIgniter or Laravel, we define a model by class, that is, in a model we only have one class stored and that's it; but in Django, specifically in Python this is not the case and this is very cool.

A huge advantage that we have when creating classes such as models or forms in Django is that we can define all the structures, that is, the classes, in a single file, therefore we can have great control over each of the layers or sublayers of our project. .

And this is due to the great modularization and organization when importing a class, function or variable from Python.

Create a model in Django

Finally we are going to create a model; a model is simply a class in Python that defines attributes where each attribute is generally a field of our object that this will later translate to as a column in the database, but we'll get to that later.

Field types

We have multiple types of fields that we can use, for text type, numbers, boolean files, images and a long etc:

You can see more in the official documentation

So, finally we are going to create a model for example one that we created in our great course on Django that looks like this:

class Comment(models.Model):
    text = models.TextField()
    date_posted = models.DateTimeField(auto_now_add=True)
    element = models.ForeignKey(Element, related_name='comments', on_delete=models.CASCADE, NULL=True)
 
    def __str__(self):
        return 'Comentario #{}'.format(self.id)

Explanation of the above code

So, we have a property for the comment text and a date, simple as that; but as you can see, we can also define attributes in the fields, depending on the type of field we can define one or the other attributes or options, as in the case of auto_now_add that receives a boolean to indicate that as soon as we create this record in the database data take the current system date.

As you can see, we simply have to specify the field type with the corresponding function, for example TextField for an open text field, or a DateTimeField field for a date field, we specify the name of the column that would be the same name of the attribute and ready.

And with this we create our first model in Django; in the next posts we will see what else we can do with this new element in Django.

We also specify the type of relationship, which in this case is of the cascade type.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.