duplicate Key violation in Django insert doing after Django update

﹥>﹥吖頭↗ 提交于 2020-07-23 06:09:41

问题


first i update my model instance, after that i tried to insert a new data but showing

"IntegrityError('duplicate key value violates unique constraint "RFIDActivation_ActivationId_key"\nDETAIL: Key ("ActivationId")=(6de9ed9a) already exists.\n',)"

Models.py

class RFIDActivation(models.Model):
    RFIDActivationId = models.AutoField(primary_key=True, db_column='RFIDActivationId')
    Device = models.ForeignKey(Device, on_delete=models.CASCADE, db_column='DeviceId')
    Employee = models.ForeignKey(Employee, on_delete=models.CASCADE, db_column='EmployeeId')
    ActivationId = models.CharField(max_length=10, unique=True, default=uuid4().hex[:8])
    ActivationStatus = models.CharField(max_length=1)default=None)

    class Meta:
       db_table = "RFIDActivation"

my serializer.py

class RFIDActivationSerializer(serializers.ModelSerializer):
    class Meta:
        model = RFIDActivation
        fields = '__all__'

view.py

@api_view(["POST"])
@permission_classes([IsAuthenticated])
def rfid_activation_initial(request):
     RFIDActivation.objects.filter(Employee=request.POST.get("Employee")).update(
            ActivationStatus='2',
            ActivationMessage='Abort'
        )

    rfid_activation = {
        'Employee': request.POST.get("Employee"),
        'Device': request.POST.get("Device"),
        'ActivationStatus': "0",
        'ActivationMessage': "RFID Activation Initiated"
    }
    rfid_serializer = RFIDActivationSerializer(data=rfid_activation)
    if rfid_serializer.is_valid():
        rfid_serializer.save()

but rfid_serializer.save() shows exceptionexception

Previous ActivationId is using to saving the new data. ActivationId is unique and auto generated. How can i solve this. Error is showing trying to insert after update query

view.py for working fine code only insertion.

@api_view(["POST"])
@permission_classes([IsAuthenticated])
def rfid_data_add(request):
    rfid_activation = {
        'Employee': request.POST.get("Employee"),
        'Device': request.POST.get("Device"),
        'ActivationStatus': "0",
        'ActivationMessage': "RFID Activation Initiated"
    }
    rfid_serializer = RFIDActivationSerializer(data=rfid_activation)
    if rfid_serializer.is_valid():
        rfid_serializer.save()

回答1:


In restframework, serializer.save() has different behavior based on creation and update. If you pass instance to serializer, it will update the object, otherwise, it will create a new one

@api_view(['POST', 'PUT'])
def rfid_data(request, pk):
    """
    Update or delete a rfid data.
    """

    rfid_activation = {
         ...
    }

    if request.method == 'POST':
        rfid_serializer = RFIDActivationSerializer(data=rfid_activation)
        if rfid_serializer.is_valid():
            rfid_serializer.save()
            return Response(rfid_serializer.data,status=status.HTTP_201_CREATED)
        return return Response(rfid_serializer.data, status=status.HTTP_400_BAD_REQUEST)

    # PUT is used for object update
    elif request.method == 'PUT':
        rfid = RFIDActivation.objects.get(pk=pk)
        # pass instance to your serializer
        # pass partial=True to allow partial updates
        rfid_serializer = RFIDActivationSerializer(instance=rfid, data=rfid_activation, partial=True)
        if rfid_serializer.is_valid():
            rfid_serializer.save()
            return Response(rfid_serializer.data)
        return Response(rfid_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

UPDATE

default is calculated at import time, in your case, your default value for activation_id is calculated once and never change for subsequent instances. You have to define a function, thus default callable will be updated each time on object creation, read more here

def activation_id_generator(self):
    return uuid4().hex[:8]

# then in your class
class RFIDActivation(models.Model):
    ...
    ActivationId = models.CharField(max_length=10, unique=True, default=activation_id_generator)


来源:https://stackoverflow.com/questions/62672084/duplicate-key-violation-in-django-insert-doing-after-django-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!