Models - Django Online Store - 04

The next step we are going to do would be once the application is created in Django to create the models in case you do not want to do it manually, remember that all this is also in the associated repository:

https://github.com/libredesarrollo/libro-curso-django-base

The configuration made for the application will be the following:

elements/models.py

from django.db import models


class Category(models.Model):
    title = models.CharField(max_length=255)
    slug = models.CharField(max_length=255)


    def __str__(self):
        return self.title
    
class Type(models.Model):
    title = models.CharField(max_length=255)
    slug = models.CharField(max_length=255)


    def __str__(self):
        return self.title
    
class Element(models.Model):
    title = models.CharField(max_length=255)
    slug = models.CharField(max_length=255)


    description = models.TextField()
    content = models.TextField()


    price = models.DecimalField(max_digits=10, decimal_places=2, default=0.0) # 12345678.10


    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    type = models.ForeignKey(Type, on_delete=models.CASCADE)


    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.title

You can see that it is a normal configuration that we would do for posts and categories, that is, for a blog, only in this case, as I was telling you, when dealing with an entity like
who says generic, I called it elements and so that we can specify what the type is, as I told you before, if it is a publication, if it is a product, etc.

Here we define the type in which we can define, excuse the redundancy, the type that we want to create for one element at a time, but in the end it has the typical things like the title, the slug, the description, a price, which if it is a publication should not have a category, the type of course, and the dates. That is all, so for the rest, it is the same here, title

Remember that by default we are using SQLite, which is the engine that I am going to leave because I do not care about the database, you really configure the one you prefer.

We create and execute the migrations:

$ python manage.py makemigrations
$ python manage.py migrate

I agree to receive announcements of interest about this Blog.

We create the models for the product, category and partners.

- Andrés Cruz

En español

This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y Libro desarrollo web con Django 5 y Python 3 + integración con Vue 3, Bootstrap y Alpine.js.

) )