getting Django current user on iOS app

237 views Asked by At

I'm new to Django, I'm starting a Django app, I'm using the registration app and Django rest framework, this app will also serve as the backend for an iOS app, I already manage to create users from the iOS app and edit User Profiles from the browser, I need help to find an approach that lets the user edit his profile details once he is logged in. I tried setting a lookup_field like "user.username" to the UserProfileViewSet so I could access the UserProfile object and edit it, but that didn't work.

I also thought I could somehow return the user id after logging in and use this id to reference the userprofile I wanna edit but it does not seem practical at all. What do you think??

Other way I found to do this is by calling user.get_profile() but I don't know how I can make this work from the iOS app.

Here is my UserProfile Model, and serializers, any help would be great. Thanks

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')

I create the UserProfile using a signal like this

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)
2

There are 2 answers

0
Andrespch On BEST ANSWER

For everyone trying to get the profile for a user this is the way I managed to do it thanks to Kevin's help.

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)

Using a url like this

url(r'^api/current-user',CurrentUserView.as_view(),
        name="current_user"),

You will get an answer like this

"id": 1, 
    "username": "username", 
    "email": "[email protected]", 
    "profile": {
        "id": 1, 
        "user": 1, 
        "bio": "My Bio", 
        "sex": "M", 
        "birthdate": "1987-12-02"
    }
8
Kevin Stone On

You didn't post your ViewSet code, so I have to guess. But your model should be set to User and your lookup_field should be set to username.

Something like:

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