from rest_framework import viewsets
from api import models , serializers
from django.db.models import Max

class TipoCargoViewSet(viewsets.ModelViewSet):
    queryset = models.TipoCargo.objects.all()
    serializer_class = serializers.TipoCargoSerializer

    def create(self, request, *args, **kwargs):
        # se indica que si no se indico el id, entonces sea el maximo + 1
        if not request.data.get('id_cargo', None):
            max_id = models.TipoCargo.objects.aggregate(Max('id_cargo'))['id_cargo__max']
            new_id = max_id + 1 if max_id is not None else 1
            request.data['id_cargo'] = new_id
        
        return super().create(request, *args, **kwargs)