Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Django rest framework get data based on current userId/token

Jin Nii Sama:

In my model, i have a family table where user are able to enter their family details and store their information. But how do i get/query all the family member based on current Userid ?

For eg: userId=1 added 2 family member, mother and father. How do i get these 2 family member based on the query of the current user's userId ?

here is my code :


class MyUser(AbstractUser):
userId = models.AutoField(primary_key=True)
gender = models.CharField(max_length=6, blank=True, null=True)
nric = models.CharField(max_length=40, blank=True, null=True)
birthday = models.DateField(blank=True, null=True)
birthTime = models.TimeField(blank=True, null=True)

class Family(models.Model):
userId = models.ForeignKey(MyUser)
relationship = models.CharField(max_length=100)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
gender = models.CharField(max_length=6, blank=True, null=True)


class MyUserSerializer(serializers.ModelSerializer):
valid_time_formats = ['%H:%M', '%I:%M%p', '%I:%M %p']
birthTime = serializers.TimeField(format='%I:%M %p', input_formats=valid_time_formats, allow_null=True, required=False)

class Meta:
model = MyUser
fields = ['userId', 'username', 'email', 'first_name', 'last_name', 'gender', 'nric', 'birthday', 'birthTime']
read_only_fields = ('userId',)
extra_kwargs = {"password": {"write_only": True}}

def update(self, instance, validated_data):
for attr, value in validated_data.items():
if attr == 'password':
setattr(instance, attr, value)
return instance

class FamilySerializer(serializers.ModelSerializer):

class Meta:
model = Family
fields = ('id', 'userId', 'first_name', 'last_name', 'gender', 'relationship')


class MyUserViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = MyUser.objects.all()
serializer_class = MyUserSerializer
filter_backends = (filters.SearchFilter,)
search_fields = ('=userId', 'username', 'email', 'first_name', 'last_name')

class FamilyViewSet(viewsets.ModelViewSet):
permission_classes = [AllowAny]
queryset = Family.objects.all()
serializer_class = FamilySerializer

def get_object(self, *args, **kwargs):
return self.request.user

def perform_update(self, serializer):
super(CurrentFamilyView, self).perform_update(serializer)
user = serializer.instance
if settings.SEND_ACTIVATION_EMAIL and not user.is_active:
context = {'user': user}
to = [MyUser(user)]
email.ActivationEmail(self.request, context).send(to)

Can anyone help me with get current user's family

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered

This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Django rest framework get data based on current userId/token


Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription