【问题标题】:getting Django current user on iOS app在 iOS 应用程序上获取 Django 当前用户
【发布时间】:2014-03-19 00:02:04
【问题描述】:

我是 Django 新手,我正在启动一个 Django 应用程序,我正在使用注册应用程序和 Django rest 框架,这个应用程序也将作为 iOS 应用程序的后端,我已经设法从iOS应用程序并从浏览器编辑用户配置文件,我需要帮助找到一种方法,让用户在登录后编辑他的配置文件详细信息。我尝试将“user.username”之类的lookup_field设置为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')

我使用这样的信号创建用户配置文件

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)

【问题讨论】:

    标签: ios django django-rest-framework lookup-field


    【解决方案1】:

    你没有发布你的ViewSet 代码,所以我不得不猜测。但是你的model 应该设置为User,你的lookup_field 应该设置为username

    类似:

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

    【讨论】:

    • 好的,但是我应该使用什么方法呢?如何从 iOS 应用程序获取当前用户?谢谢
    • 我想我只是找到了一种暂时避免处理这个问题的方法,我认为我应该使用 AbstractUser 而不是使用 UserProfile 模型,对吗?
    • 是的,您可以向抽象用户添加字段,而不是引用配置文件。通常配置文件更具可扩展性,尤其是在您可能拥有不同类型或类别的用户时。
    • 要获取当前用户,需要在你的ViewSet中添加一个方法来检索当前用户(存储在django中request.user下)。
    • 我建议检索用户,然后检索配置文件。您的UserProfile 应该添加一个related_name 选项,以便您可以引用它:user = models.OneToOneField(User, related_name='profile')。 (unique=True 对于 OneToOneFields 是多余的)。然后您可以使用属性查找来获取配置文件:profile = User.objects.get(pk=<whatever>).profile)
    【解决方案2】:

    感谢 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(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"
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2012-03-14
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 2021-11-20
      相关资源
      最近更新 更多