Skip to main content

Overview

The products module contains models for managing the restaurant’s product catalog, including products, categories, brands, suppliers, stock tracking, and product images.

Producto

Main product model representing items available for sale.
id_producto
AutoField
required
Primary key for the product. Auto-generated.
nombre
CharField
required
Product name. Must be unique.
  • max_length: 50
  • unique: True
  • Validation: Cannot be empty or contain only whitespace
precio_compra
DecimalField
required
Purchase price of the product.
  • max_digits: 10
  • decimal_places: 2
  • Validation: Must be less than precio_venta
precio_venta
DecimalField
required
Sale price of the product.
  • max_digits: 10
  • decimal_places: 2
  • Validation: Must be greater than precio_compra
stock
IntegerField
Current stock quantity.
  • default: 0
  • Validation: Cannot be negative
  • Note: Changes to this field automatically create a Stock record
descripcion
TextField
Product description.
  • blank: True
activo
BooleanField
Whether the product is active/available.
  • default: True
id_categoria
ForeignKey
Reference to product category.
  • to: Categoria
  • on_delete: SET_NULL
  • null: True
  • blank: True
id_proveedor
ForeignKey
Reference to product supplier.
  • to: Proveedor
  • on_delete: SET_NULL
  • null: True
  • blank: True
id_marca
ForeignKey
Reference to product brand.
  • to: Marca
  • on_delete: SET_NULL
  • null: True
  • blank: True

Methods

Database

  • Table name: producto
  • Related names: stocks (reverse relation from Stock), imagenes (reverse relation from ProductoImagen)

Categoria

Product category model.
id_categoria
AutoField
required
Primary key for the category.
nombre_categoria
CharField
Category name.
  • max_length: 50
  • null: True
  • blank: True
descripcion
TextField
Category description.
  • null: True
  • blank: True

Database

  • Table name: categoria
  • Verbose names: Categoría (singular), Categorías (plural)

Marca

Product brand model.
id_marca
AutoField
required
Primary key for the brand.
marca
CharField
required
Brand name.
  • max_length: 50
descripcion
TextField
Brand description.
  • null: True
  • blank: True

Database

  • Table name: marca
  • Verbose names: Marca (singular), Marcas (plural)

Proveedor

Supplier/vendor model.
id_proveedor
AutoField
required
Primary key for the supplier.
nombre
CharField
required
Supplier name.
  • max_length: 50
contacto
CharField
required
Contact person name.
  • max_length: 50
telefono
BigIntegerField
Contact phone number.
  • null: True
  • blank: True
direccion
TextField
required
Supplier address.

Database

  • Table name: proveedor
  • Verbose names: Proveedor (singular), Proveedores (plural)

Stock

Stock history tracking model. Records all stock changes for products.
id_stock
AutoField
required
Primary key for the stock record.
id_producto
ForeignKey
Reference to the product.
  • to: Producto
  • on_delete: SET_NULL
  • null: True
  • blank: True
  • db_column: ‘id_producto’
  • related_name: ‘stocks’
cantidad
IntegerField
Stock quantity at this point in time.
  • null: True
  • blank: True
fecha_hora
DateTimeField
Timestamp when the stock record was created.
  • auto_now_add: True

Database

  • Table name: stock
  • Verbose names: Stock (singular), Stocks (plural)
Stock records are automatically created when a Producto’s stock field is modified. This provides a complete audit trail of inventory changes.

ProductoImagen

Product image model. Supports multiple images per product.
id_imagen
AutoField
required
Primary key for the image.
producto
ForeignKey
required
Reference to the product.
  • to: Producto
  • on_delete: CASCADE
  • related_name: ‘imagenes’
imagen
ImageField
required
Product image file.
  • upload_to: producto_image_path (custom function)
  • Images are stored in media/productos/{product_id}/
  • Automatically converted to WebP format
  • help_text: ‘Imagen del producto (se guardará en media/productos/)‘

Database

  • Table name: producto_imagen
  • Verbose names: Imagen del producto (singular), Imágenes de productos (plural)

Image Processing


Validators


Usage Examples

Creating a Product

from products.models import Producto, Categoria, Marca, Proveedor

# Create category, brand, and supplier
categoria = Categoria.objects.create(
    nombre_categoria="Bebidas",
    descripcion="Bebidas alcohólicas y no alcohólicas"
)

marca = Marca.objects.create(
    marca="Coca-Cola",
    descripcion="Empresa de bebidas"
)

proveedor = Proveedor.objects.create(
    nombre="Distribuidora ABC",
    contacto="Juan Pérez",
    telefono=3001234567,
    direccion="Calle 123 #45-67"
)

# Create product
producto = Producto.objects.create(
    nombre="Coca-Cola 350ml",
    precio_compra=1500.00,
    precio_venta=3000.00,
    stock=50,
    descripcion="Bebida gaseosa sabor cola",
    activo=True,
    id_categoria=categoria,
    id_marca=marca,
    id_proveedor=proveedor
)

Updating Stock

# Update stock (automatically creates Stock record)
producto.stock = 75
producto.save()

# Get current stock
stock_actual = producto.stock_actual()

# View stock history
historial = producto.stocks.order_by('-fecha_hora')

Adding Product Images

from products.models import ProductoImagen

# Add image to product
imagen = ProductoImagen.objects.create(
    producto=producto,
    imagen=uploaded_file  # Django UploadedFile object
)

# Get all product images
imagenes = producto.imagenes.all()

Build docs developers (and LLMs) love