[Answered ]-How do you add validation to django rest framework api

1πŸ‘

βœ…

I think the problem here is your data models. It’s clear that a warehouse and an item have a one to many relationship. With that, you would have something like this in your models.

from django.db import models


class Warehouse(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=60)

    def __str__(self):
        return self.name


class Item(models.Model):
    id = models.AutoField(primary_key=True)
    warehouse = models.ForeignKey(Warehouse, related_name="items", on_delete=models.CASCADE)
    name = models.CharField(max_length=60)
    sku = models.CharField(max_length=60)
    description = models.TextField()
    price = models.DecimalField(max_digits=6, decimal_places=2)
    location = models.CharField(max_length=60)
    serial_number = models.CharField(max_length=60)

    def __str__(self):
        return self.name

The on_delete=models.CASCADE will ensure that all items related to a warehouse are deleted when a warehouse is deleted. The foreign key relationship will ensure that warehouse id you provide when creating the item, exists before the item is created.

The other files will look as follows.

serializers.py

from rest_framework import serializers

from .models import Warehouse, Item


class ItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = Item
        fields = ('id', 'name', 'sku', 'description', 'price', 'location', 'serial_number', "warehouse")


class WarehouseSerializer(serializers.HyperlinkedModelSerializer):
    items = serializers.StringRelatedField(many=True, required=False)

    class Meta:
        model = Warehouse
        fields = ('id', 'name', 'items')

views.py

from .models import Item, Warehouse
from .serializers import ItemSerializer, WarehouseSerializer

from rest_framework import viewsets


class ItemViewSet(viewsets.ModelViewSet):
    queryset = Item.objects.all().order_by('name')
    serializer_class = ItemSerializer


class WarehouseViewSet(viewsets.ModelViewSet):
    queryset = Warehouse.objects.all().order_by('name')
    serializer_class = WarehouseSerializer
πŸ‘€Josewails

Leave a comment