在iOS应用程序上获取Django当前用户

在iOS应用程序上获取Django当前用户,ios,django,django-rest-framework,lookup-field,Ios,Django,Django Rest Framework,Lookup Field,我是Django的新手,我正在启动Django应用程序,我正在使用注册应用程序和Django rest框架,该应用程序还将作为iOS应用程序的后端,我已经设法从iOS应用程序创建用户并从浏览器编辑用户配置文件,我需要帮助找到一种方法,让用户在登录后编辑他的个人资料详细信息。我尝试将“user.username”之类的lookup_字段设置为UserProfileViewSet,以便访问UserProfile对象并对其进行编辑,但没有成功 我还认为我可以在登录后以某种方式返回用户id,并使用此id

我是Django的新手,我正在启动Django应用程序,我正在使用注册应用程序和Django rest框架,该应用程序还将作为iOS应用程序的后端,我已经设法从iOS应用程序创建用户并从浏览器编辑用户配置文件,我需要帮助找到一种方法,让用户在登录后编辑他的个人资料详细信息。我尝试将“user.username”之类的lookup_字段设置为UserProfileViewSet,以便访问UserProfile对象并对其进行编辑,但没有成功

我还认为我可以在登录后以某种方式返回用户id,并使用此id引用我想要编辑的用户配置文件,但这似乎根本不实用。你觉得怎么样

我发现的另一种方法是调用user.get_profile(),但我不知道如何在iOS应用程序中实现这一点

这是我的UserProfile模型和序列化程序,任何帮助都会很好。谢谢

class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True)
# Extra attribuets
pagetitle = models.TextField(null=False)
location = models.TextField(null=True)
website = models.TextField(null=True)
bio = models.TextField(null=True)
sex = models.TextField(null=True)
birthdate = models.DateField(null=True)
def __unicode__(self):
    return "%s's profile" % self.user

def create_profile(sender, instance, created, **kwargs):
    if created:
        profile, created= UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_profile, sender=User)


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('username','email','password')

class UserProfileSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer(many=False)
    class Meta:
        model = UserProfile
        fields = ('user','bio')
我使用如下信号创建UserProfile

def create_profile(sender, instance, created, **kwargs):
    if created:
        profile, created= UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_profile, sender=User)
url(r'^api/current-user',CurrentUserView.as_view(),
        name="current_user"),
"id": 1, 
    "username": "username", 
    "email": "email@email.com", 
    "profile": {
        "id": 1, 
        "user": 1, 
        "bio": "My Bio", 
        "sex": "M", 
        "birthdate": "1987-12-02"
    }

你没有发布你的
ViewSet
代码,所以我不得不猜测。但是您的
型号
应设置为
用户
,您的
查找字段
应设置为
用户名

比如:

class UserViewSet(ModelViewSet):
    model = User
    lookup_field = "username"

对于每个试图为用户获取个人资料的人来说,多亏了Kevin的帮助,我才得以做到这一点

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        depth = 1
        fields = ('id','username','email','profile')



class CurrentUserView(APIView):
    def get(self, request):
        serializer = UserSerializer(request.user)
        return Response(serializer.data)
使用这样的url

def create_profile(sender, instance, created, **kwargs):
    if created:
        profile, created= UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_profile, sender=User)
url(r'^api/current-user',CurrentUserView.as_view(),
        name="current_user"),
"id": 1, 
    "username": "username", 
    "email": "email@email.com", 
    "profile": {
        "id": 1, 
        "user": 1, 
        "bio": "My Bio", 
        "sex": "M", 
        "birthdate": "1987-12-02"
    }
你会得到这样的答案

def create_profile(sender, instance, created, **kwargs):
    if created:
        profile, created= UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_profile, sender=User)
url(r'^api/current-user',CurrentUserView.as_view(),
        name="current_user"),
"id": 1, 
    "username": "username", 
    "email": "email@email.com", 
    "profile": {
        "id": 1, 
        "user": 1, 
        "bio": "My Bio", 
        "sex": "M", 
        "birthdate": "1987-12-02"
    }

好吧,那我应该用什么方法呢?如何从iOS应用程序获取当前用户?谢谢,我想我现在找到了一种避免处理这个问题的方法,我想我应该使用用户AbstractUser,而不是使用UserProfile模型,对吗?是的,你可以向抽象用户添加字段,而不是引用配置文件。通常,配置文件更具可扩展性,尤其是当您可能有不同类型或类别的用户时。要获取当前用户,您需要在
视图集中添加一个方法来检索当前用户(存储在django中的
request.user
)。我建议先检索用户,然后再检索配置文件。您的
UserProfile
应该添加一个
related\u name
选项,以便您可以引用它:
user=models.OneToOneField(user,related\u name='profile')
。(unique=True对于OneToOneFields是多余的)。然后您可以使用属性查找来获取配置文件:
profile=User.objects.get(pk=).profile)