Creating product catalog models

The catalog of our shop will consist of products that are organized into different categories. Each product will have a name, optional description, optional image, price, and availability. Edit the models.py file of the shop application that you just created and add the following code:

from django.db import modelsclass Category(models.Model):    name = models.CharField(max_length=200,                            db_index=True)    slug = models.SlugField(max_length=200,                            unique=True)    class Meta:        ordering = ('name',)        verbose_name = 'category'        verbose_name_plural = 'categories'    def __str__(self):        return self.nameclass Product(models.Model):    category = models.ForeignKey(Category,                                 related_name='products',                                 on_delete=models.CASCADE) name = models.CharField(max_length=200, ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.