0

这是我的models.py

class Cfituser(models.Model):
user = models.OneToOneField(User)
socialid = models.IntegerField(null=True)
accesstoken = models.CharField(max_length=255L, null = True)
class Meta:
    db_table = 'CfitUser'
def __str__(self):  
      return "%s's profile" % self.user  

@receiver(post_save, sender=User)
def create_cfituser(sender, instance, created, **kwargs):  
    if created:  
        Cfituser.objects.get_or_create(user=instance) 

这是我的意见.py

@api_view(['GET', 'POST'])
def users_create(request, format = None):
"""
List all users, or create a new user.
"""
if request.method == 'GET':
    cfituser = Cfituser.objects.all()
    serializer = CfituserSerializer(cfituser, many=True)
    return Response(serializer.data)

elif request.method == 'POST':
    serializer = CfituserSerializer(data=request.DATA)
    if serializer.is_valid():
            print serializer.data
            user = User.objects.create_user(username = serializer.data['socialid'])
            cfituser = Cfituser.objects.get(user = user)
            cfituser.accesstoken = serializer.data['accesstoken']
            cfituser.socialid = serializer.data['socialid']
            cfituser.save()
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
    else:
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

每当有 HTTP Post 请求时,我的数据库表都会按预期填写,但此错误不断弹出。我已经尝试了几乎所有可用的解决方案,但我坚持这一点。

我已经尝试过user = models.OneToOneField(User, null = True),但这会导致我的数据库表中有两个条目,一个带有user_id = NULL,一个带有user_id = actualvalue.

对于如何解决这个问题,有任何的建议吗?

4

2 回答 2

1

默认情况下,保存会将条目提交到数据库,以防止这种情况发生,传递commit=Falsesave(),然后进行自定义。

 serializer = serializer.save(commit=False)

 user, created = User.objects.get_or_create(username = serializer.socialid) 
 cfituser, created = Cfituser.objects.get_or_create(user = user)
 # cfituser.user = user This line is unnecessary
 cfituser.accesstoken = serializer.accesstoken
 cfituser.socialid = serializer.socialid

 cfituser.save()
 serializer.save()

你也在重复你的努力,因为你的信号也会尝试创建一个用户。如果您使用的是 django 1.5,请使用自定义用户模型;对于社会登记/oauth,使用django-social-auth.

于 2013-08-28T06:08:29.247 回答
0

OneToOneField意味着,在Cfituser.user关系的反面将直接返回一个对象(user.cfituser给出Cfituser)。SOCfituser.user在整个表格中必须是唯一的(一个且只有一个)。

class Cfituser(models.Model):
    user = models.OneToOneField(User)
    socialid = models.IntegerField(null=True)
    accesstoken = models.CharField(max_length=255L, null = True)
    def __unicode__(self):  
         return "%s's profile" % self.user.username  


def users_create(request, format = None):
"""
List all users, or create a new user.
"""
    if request.method == 'GET':
        cfituser = Cfituser.objects.all()
        serializer = CfituserSerializer(cfituser, many=True)
        return Response(serializer.data) 
    elif request.method == 'POST':
        serializer = CfituserSerializer(data=request.DATA)
        if serializer.is_valid():
            print serializer.data
            user = User.objects.create_user(username = serializer.data['socialid'])                
            Cfituser(user = user,accesstoken = serializer.data['accesstoken'],socialid = serializer.data['socialid']).save()
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
    else:
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
于 2013-08-28T06:03:43.593 回答