【问题标题】:Is there any method to get rid of this error. (1062, "Duplicate entry '2' for key 'customer_customerprofile.user_id'")?有什么方法可以摆脱这个错误。 (1062,“密钥'customer_customerprofile.user_id'的重复条目'2'”)?
【发布时间】:2021-04-30 04:04:26
【问题描述】:

我必须在这个程序中创建一个从 lat 和 on 称为 location 的字段。位置打印正确,但保存时出现重复输入错误。 当 post 功能起作用时,我需要将 user_id 更新为 id 。由于保存了两次,所以出现了重复输入错误。

def post(self, request, format=None):
            serializer = CustomerSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                lat = serializer.data.get('latitude',None)
                lon=serializer.data.get('longitude',None)
                lat_1=float(lat)
                lon_1=float(lon)
                location=Point((lat_1,lon_1),srid=4326)
                print(location)
                id=serializer.data.get('user')
                print(id)
                v=CustomerProfile(location=location,user_id=id)
                v.save()
                return Response({"message":"Customer Profile Updated Successfully","data":serializer.data}, status=200)
            return Response({"message":"Customer registration failed!!","data":serializer.data}, status=400)

用户模型

class CustomUser(AbstractUser):
    username = models.CharField(max_length=10,unique=True,blank=True,null=True)
    email = models.EmailField(_('email address'), unique=True)
    phone = models.CharField(max_length=13)

    is_admin = models.BooleanField(default=False)
    is_service_provider = models.BooleanField(default=False)
    is_customer = models.BooleanField(default=False)


    # is_active=models.BooleanField(default=False)
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['phone','email']

客户档案模型

class CustomerProfile(models.Model):
    user = models.OneToOneField(CustomUser,on_delete=models.CASCADE)
    location = models.PointField(default=Point(0,0),geography=True)
    latitude = models.DecimalField(blank=True,max_digits=9,decimal_places=6,default=None,null=True)
    longitude = models.DecimalField(blank=True,max_digits=9,decimal_places=6,default=None,null=True)

【问题讨论】:

  • 请添加您的CustomerProfileUser 模型。
  • 请看一下。

标签: django rest django-rest-framework


【解决方案1】:

您创建了新实例,而不是我们获取(v=CustomerProfile.objects.get(id=id)) 这个用户并更新了。

def post(self, request, format=None):
            serializer = CustomerSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                lat = serializer.data.get('latitude',None)
                lon=serializer.data.get('longitude',None)
                lat_1=float(lat)
                lon_1=float(lon)
                location=Point((lat_1,lon_1),srid=4326)
                print(location)
                id=serializer.data.get('user')
                print(id)
                v=CustomerProfile.objects.get(id=id)
                v.location = location
                v.save()
                return Response({"message":"Customer Profile Updated Successfully","data":serializer.data}, status=200)
            return Response({"message":"Customer registration failed!!","data":serializer.data}, status=400)

【讨论】:

  • UnicodeDecodeError at /api/customer 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte Im得到这个错误。
  • 能否请您通过 pastebin 分享完整的堆栈跟踪
猜你喜欢
  • 2011-03-17
  • 1970-01-01
  • 2011-11-15
  • 2020-10-11
  • 2023-01-20
  • 2011-07-13
  • 2020-02-05
  • 2011-05-19
  • 2017-11-28
相关资源
最近更新 更多