Skip to main content

Overview

The Notificacion model stores user notifications that are delivered via WebSocket connections for real-time updates.

Notificacion Model

Stores notification messages for users with read/unread status.

Fields

usuario
ForeignKey
required
Reference to Django User model (CASCADE on delete). The user who receives this notification.
mensaje
TextField
required
The notification message content
leida
BooleanField
Whether the notification has been read. Default: False
fecha
DateTimeField
When the notification was created. Auto-set on creation.

String Representation

def __str__(self):
    return f"{self.usuario.username} - {self.mensaje}"

Usage Examples

Create Notification

from notifications.models import Notificacion
from django.contrib.auth.models import User

user = User.objects.get(username='maria')

# Create notification
notificacion = Notificacion.objects.create(
    usuario=user,
    mensaje='Nuevo pedido asignado a mesa 5'
)

Query Notifications

from notifications.models import Notificacion

# Get unread notifications for user
unread = Notificacion.objects.filter(
    usuario=request.user,
    leida=False
).order_by('-fecha')

# Get all notifications
all_notifications = Notificacion.objects.filter(
    usuario=request.user
).order_by('-fecha')

# Count unread
unread_count = Notificacion.objects.filter(
    usuario=request.user,
    leida=False
).count()

Mark as Read

# Mark single notification as read
notification = Notificacion.objects.get(id=notification_id)
notification.leida = True
notification.save()

# Mark all as read for user
Notificacion.objects.filter(
    usuario=request.user,
    leida=False
).update(leida=True)

# Mark specific notifications as read
notification_ids = [1, 2, 3]
Notificacion.objects.filter(
    id__in=notification_ids,
    usuario=request.user
).update(leida=True)

Delete Old Notifications

from django.utils import timezone
from datetime import timedelta

# Delete notifications older than 30 days
thirty_days_ago = timezone.now() - timedelta(days=30)
Notificacion.objects.filter(
    fecha__lt=thirty_days_ago
).delete()

Helper Function

The notifications app provides a helper function for sending notifications:
notifications/utils.py
from notifications.models import Notificacion
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync

def notificar_usuario(usuario, mensaje):
    """
    Send notification to user via database and WebSocket
    
    Args:
        usuario: User instance
        mensaje: Notification message string
    """
    # Save to database
    notificacion = Notificacion.objects.create(
        usuario=usuario,
        mensaje=mensaje
    )
    
    # Send via WebSocket
    channel_layer = get_channel_layer()
    async_to_sync(channel_layer.group_send)(
        f'user_{usuario.id}',
        {
            'type': 'notificacion_mensaje',
            'message': mensaje
        }
    )
    
    return notificacion
Usage:
from notifications.utils import notificar_usuario
from django.contrib.auth.models import User

user = User.objects.get(username='maria')
notificar_usuario(user, 'Tu pedido ha sido confirmado')

Common Use Cases

Order Status Updates

from notifications.utils import notificar_usuario
from tables.models import Pedido

pedido = Pedido.objects.get(id=pedido_id)

# Notify all users associated with the order
for user in pedido.usuarios.all():
    notificar_usuario(
        user,
        f'Pedido #{pedido.id} actualizado a estado: {pedido.get_estado_display()}'
    )

Low Stock Alerts

from products.models import Producto
from notifications.utils import notificar_usuario
from django.contrib.auth.models import User

# Check for low stock
low_stock_products = Producto.objects.filter(stock__lt=10, activo=True)

if low_stock_products.exists():
    # Notify administrators
    admins = User.objects.filter(is_staff=True)
    
    for admin in admins:
        for producto in low_stock_products:
            notificar_usuario(
                admin,
                f'Alerta: Stock bajo para {producto.nombre} (quedan {producto.stock} unidades)'
            )

Expense Approval

from expenses.models import Expense
from notifications.utils import notificar_usuario

expense = Expense.objects.get(id=expense_id)

# Notify managers for approval
managers = User.objects.filter(userprofile__rol__nombre='Gerente')

for manager in managers:
    notificar_usuario(
        manager,
        f'Nuevo gasto registrado: ${expense.amount} en {expense.category.name}'
    )

REST API Endpoints

Get Notifications

GET /notifications/api/list/
Returns JSON list of user’s notifications:
{
  "notifications": [
    {
      "id": 1,
      "mensaje": "Nuevo pedido asignado",
      "leida": false,
      "fecha": "2025-03-06T10:30:00Z"
    }
  ],
  "unread_count": 5
}

Mark as Read

POST /notifications/api/mark-read/
Content-Type: application/json

{
  "ids": [1, 2, 3]  // Empty array marks all as read
}

WebSocket Integration

Notifications are delivered in real-time via WebSocket. See the Notifications Documentation for complete WebSocket setup and frontend integration. WebSocket URL:
ws://localhost:8000/ws/notificaciones/

Database Queries Performance

Optimize Notification Queries

# Use select_related for user info
notifications = Notificacion.objects.filter(
    leida=False
).select_related('usuario').order_by('-fecha')

# Pagination for large result sets
from django.core.paginator import Paginator

all_notifications = Notificacion.objects.filter(
    usuario=request.user
).order_by('-fecha')

paginator = Paginator(all_notifications, 20)  # 20 per page
page = paginator.get_page(page_number)

Create Index for Performance

# In notifications/models.py
class Notificacion(models.Model):
    # ... fields ...
    
    class Meta:
        indexes = [
            models.Index(fields=['usuario', 'leida', '-fecha']),
        ]

Notifications System

Complete WebSocket notification setup

API Overview

REST API endpoints

Build docs developers (and LLMs) love