Return id of new Item created with POST using ModelSerializer and ModelViewSet in Django Rest Framework

2.3k views Asked by At

I'd like to get the new id as a response after making a Post in Django Rest Framework.

Intended function: User gets a json response with the hyperlink to the new item created.

POST

{
    "label": "BMW M3",
    "price": 5000.00
}

Response:

 {
        "id-url": "www.website.com/api/products/id/"
    }

I am aware this topic been covered in various ways but I am looking for a minimal (but complete) example using a ModelSerializer, I have not be able to find this yet, thus my question.

My model:

class Product(models.Model):
    label = models.CharField(max_length=50, blank=True, null=True)
    price = models.FloatField(blank=True, null=True)

My serializer:

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = '__all__'

My view:

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

My urls:

router = routers.DefaultRouter()
router.register('products', ProductViewSet)

urlpatterns = [
    path('', include(router.urls)),
]

Attempts:

Some references I have attempted. Here is similar question but the answers does seems incomplete ex_0 I can re-create this ex_1, but does not give me the id

2

There are 2 answers

0
Jaco On BEST ANSWER

Thanks to this post: Get the id of the object recently created Django Rest Framework I managed to come up with a potential solution. It seems to work but I am a bit unsure if it is the "best" way or not.

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

    def create(self, request):
        serializer = ProductSerializer(data=request.data)
        if serializer.is_valid():
            product = serializer.save()
            pid = product.id
            product.url = f'http://localhost:8000/api/products/{pid}/'
            data = serializer.data
            data.update({'id-url':product.url}) # attaching key-value to the dictionary
            return Response(data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 
0
Kraig Hanson On
class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

    def create(self, request, *args, **kwargs):
        response = super(ProductViewSet, self).create(request, *args, **kwargs)
        # response.data has everything specified in your serializer; it seems fine to clobber it
        # alternatively, you could add to it: response.data['foo']='baz'
        response.data = {'id-url': f'http://localhost:8000/api/products/{response.data['id']}/'}
        return response