Search code examples
djangodjango-modelsdjango-rest-frameworkdjango-viewsdjango-serializer

How to manually change value of primary key in Django Model through serializers?


Is it somehow possible to manually change ID value in AutoField (primary key) through serializer?
I found this in documentation of Django: https://docs.djangoproject.com/en/4.0/ref/models/instances/#auto-incrementing-primary-keys, but I have no idea how to do this including serializers.

My task is: If car with given ID does not exists, fetch car from external API and save it.

Model:

from django.db import models

class Car(models.Model):
    speed = models.IntegerField(default=1)
    color = models.CharField(max_length=200)
    wheels = models.CharField(max_length=500)

My serializer:

from rest_framework import serializers
from .models import Car


class CarSerializer(serializers.ModelSerializer):
    class Meta:
        model = Car
        fields = ['id', 'speed', 'color', 'wheels']

My view:

@api_view(['GET', 'PUT', 'DELETE'])
def car_detail_id(request, id):
    try:
        car = Car.objects.get(pk=id)
    except Car.DoesNotExist:
        #-----------------------------------------------------------#
        car = search_car(id) #fetch car from external API
        if request.method == "GET" and car is not None:
            serializer = CarSerializer(data=car)
            if serializer.is_valid():
                serializer.save()
                return Response(status=status.HTTP_201_CREATED)
        #-----------------------------------------------------------#
        return Response(status=status.HTTP_404_NOT_FOUND)
    return car_detail(request, "id", car) #method to handle CRUD

Car structure:

 {
    "speed": 1,
    "id": 1,
    "color": "Green",
    "wheels": "Black"
  },

This implementation fetch car with given ID from API, then saves it, but with different ID. Are there any better ways to do this? Let me know below!


Solution

  • If you are using serializers.ModelSerializer like your above example class CarSerializer(serializers.ModelSerializer):. You can set extra properties while saving.

    serializer = CarSerializer(data=car)
    if serializer.is_valid():
        serializer.save(id=id)
    

    You can add other properties as long as it is in the model. I can't find any other solution to do it.

    I found this way from here Solution. Hope this will helps