I have 2 model, MyUser, Family.

What i want to do is display users details in Family table based on the 
MyUser table. It is like adding friends or family in other social media and 
display them

In my current Family table, there are these 3 field. userId , familyId and 
relationship

userId(foreign key from MyUser table) = current user
familyId(foreign key from MyUser table) = current user's family member
relationship = who is the family member to the current user (eg: mother, 
father)

But i want to display **only some of** userId and familyId details from 
MyUser so the result will be like this:

**expected result**

    userId = { id = 1, username = tom, image = 
"/media/images1/asdasdsda000.PNG"}
    familyId = { id = 2, username = steve, image = 
"/media/images1/asdasdsda111.PNG"}
    relationship = father
             
**current result**

    userId = 1
    familyId = 2
    relationship = father

How do i do it ? I thought about nested serializer but i only want to 
display user details but not edit it from there. 

Here is my code:

**models.py**

    class MyUser(AbstractUser):
        userId = models.AutoField(primary_key=True)
        image1 = models.ImageField(upload_to='images1')
        dob = models.DateField(blank=True, null=True)
        gender = models.CharField(max_length=6)

    class Family(models.Model):
        userId = models.ForeignKey(MyUser)
        familyId = models.ForeignKey(MyUser)
        relationship = models.CharField(max_length = 20)

**serializer.py**

    class MyUserSerializer(serializers.ModelSerializer):
        class Meta:
            model = MyUser
            fields = ['userId', 'username', 'email', 'first_name', 
'last_name', 'image']
            read_only_fields = ('userId',)
            extra_kwargs = {"password": {"write_only": True}}

    class FamilySerializer(serializers.ModelSerializer):
        class Meta:
            model = Family
            fields = ('id', 'userId', 'familyId', 'relationship')

**views.py**

    class MyUserViewSet(viewsets.ModelViewSet):
        permission_classes = [AllowAny]
        queryset = MyUser.objects.all()
        serializer_class = MyUserSerializer

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/59b03f60-c230-4fb3-817b-600883fda73f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to