Django Rest框架:错误“具有此uuid的用户已存在。”

2024-09-28 19:02:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个模型,并将uuid作为我的主id。当我使用POST时,它会给我错误-“具有此uuid的用户已经存在。”只有当我将用户的模型主键从“id”更改为“uuid”时,才会显示此错误

以下是相关的模型:

用户模型:

class User(models.Model):
    uuid = models.CharField(default=uuid.uuid4, max_length=50, primary_key=True)
    first_name    = models.CharField(max_length=20)
    last_name     = models.CharField(max_length=20)
    email_id      = models.EmailField(max_length=100, default="")

任命模式:

class Appointment(models.Model):

    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    zorg = models.ForeignKey(Zorg, on_delete=models.CASCADE, null=True)
    branch = models.ForeignKey(Zorg_Branche, on_delete=models.CASCADE, null=True, related_name='branch')
    timestamp = models.DateTimeField(auto_now_add=True, blank=True)
    status      = models.ForeignKey(Appointment_Status, on_delete=models.CASCADE, null=True)
    totaltime   = models.PositiveIntegerField(default=0)
    total_price = models.DecimalField(decimal_places=2, max_digits=10, default=0)

预约详情模型:

class AppointmentDetail(models.Model):
    appointment  = models.ForeignKey(Appointment, on_delete=models.CASCADE,null=True, related_name='appointment')
    service      = models.ForeignKey(Service, on_delete=models.CASCADE,null=True, related_name='service')

相关序列化程序-

UserAppointmentSerializer:

class UserAppointmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = [
                'uuid',
                'first_name',
                'last_name',
                'email_id',
                'mobile_number',
                'birthdate',
                'photo_url',
                'gender',
              ]

class ZorgBranchAppointmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Zorg_Branche
        exclude = ['id', 'zorg']

class ServiceAppointmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Service
        exclude = ['id', 'category']

class ZorgAppointmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Zorg
        fields = [
                'id',
                 'name',
                 'salon_email_id',
                 'website'
             ]

class AppointmentDetailSerializer(serializers.ModelSerializer):

    service = ServiceAppointmentSerializer()
    class Meta:
        model = AppointmentDetail
        exclude = ['id', 'appointment']

class AppointmentStatusSerilizer(serializers.ModelSerializer):

    class Meta:
        model = Appointment_Status
        fields = ['status']

class AppointmentSerializer(serializers.ModelSerializer):
    appointment = AppointmentDetailSerializer(many=True)
    status = AppointmentStatusSerilizer()
    user = UserAppointmentSerializer()
    zorg = ZorgAppointmentSerializer()
    branch = ZorgBranchAppointmentSerializer()
    class Meta:
        model = Appointment
        fields = ['id', 'status',
            'appointment',
            'user',
            'zorg',
            'branch',
            'timestamp',
            'totaltime',
            'total_price']

def create(self, validated_data):
    user = validated_data.pop('user')
    zorg = validated_data.pop('zorg')
    status = validated_data.pop('status')
    branch = validated_data.pop('branch')

    user_instance = User.objects.get(**user)
    zorg_instance = Zorg.objects.get(**zorg)
    status_instance = Appointment_Status.objects.get(**status)
    branch_instance = Zorg_Branche.objects.get(**branch)

    appointment_services = validated_data.pop('appointment')

    appointment = Appointment.objects.create(user = user_instance, zorg = zorg_instance, status = status_instance, branch = branch_instance, **validated_data)

    for services in appointment_services:
        service = services.pop('service')
        service_instance = Service.objects.get(**service)
        AppointmentDetail.objects.create(appointment=appointment, service=service_instance)

    return appointment

Tags: instancenamebranchidtruemodelsstatusservice
1条回答
网友
1楼 · 发布于 2024-09-28 19:02:12

我要检查的第一件事是您是否需要在数据库上运行makemigrationsmigrate

听起来可能是问题,因为问题发生在更改模型之后

如果这无法解决问题,请尝试使用Django的内置UUIDField而不是CharField

https://docs.djangoproject.com/en/3.1/ref/models/fields/#uuidfield

from django.db import models

class User(models.Model):
    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    # rest of your code...

我可能还建议使用Django的内置身份验证系统来管理用户

https://docs.djangoproject.com/en/3.1/topics/auth/default/

相关问题 更多 >