[Solved]Unable to render data in serializer while showing fields


model:

class User(AbstractUser):
    # user information:
    user_id = ShortUUIDField(
        length=6, max_length=6, alphabet="0123456789", primary_key=True
    )
    username = models.CharField(max_length=30, unique=True, db_index=True)
    first_name = models.CharField(max_length=50, blank=True, null=True)
    last_name = models.CharField(max_length=50, blank=True, null=True)
    email = models.EmailField(unique=True, db_index=True)
    full_name = models.CharField(max_length=100, blank=True, null=True)
    otp = models.CharField(
        unique=True, max_length=20, null=True, blank=True, db_index=True
    )
    created_at = models.DateTimeField(auto_now_add=True)
    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["username"]

    def __str__(self):
        return self.full_name

    def save(self, *args, **kwargs):
        email_user, _ = self.email.split("@")
        if self.username == "" or self.username is None:
            self.username = email_user
        if self.full_name == "" or self.full_name is None:
            self.full_name = f"{self.first_name} {self.last_name}"
        super(User, self).save(*args, **kwargs)

    def Profile(self):
        return Profile.objects.filter(user=self).first()

    def user_skills(self):
        return ProfileSkills.objects.filter(profile__user=self)

    def user_interest(self):
        return ProfileCauses.objects.filter(profile__user=self)

serializer:

class UserSerializer(serializers.ModelSerializer):
    Profile = ProfileSerializer()
    user_skills = ProfileSkillsSerializer(many=True)
    user_interest = ProfileCauseSerializer(many=True)

    class Meta:
        model = User
        fields = [
            "username",
            "first_name",
            "last_name",
            "email",
            "full_name",
            "Profile",
            "user_skills",
            "user_interest",
        ]

tried many ways but its only showing empty list

Welcome @mohammadSelimReza !

Side Note: When posting code here, enclose the code between lines of three
backtick - ` characters. This means you’ll have a line of ```, then your code,
then another line of ```. This forces the forum software to keep your code
properly formatted. (I have taken the liberty of correcting your original posts.
Please remember to do this in the future.)

Also, please don’t post images of textual information such as your json clip at the top. Copy/paste the json into the body of your post, marked as preformatted text as described above.

You have not posted the model definitions for ProfileSkills or ProfileCauses, but assuming that there is a foreign key relationship between these models and Profile, there is no need for these:

The first of these - Profile(self) seems extremely suspect. If this is a OneToOne relationship, then it should be defined as such, not as a ForeignKey being restricted by first().

Assuming that Profile is actually a one-to-one with User, then a set of ProfileSkills related to an instance of User named user would be user.profile.profileskills_set unless you’ve defined a related name for that relationship.

My first recommendation then would be to fix your models to properly identify and define the relationships among them. After that, getting the serializers to work is trivial.

thanks a lot.
actually, its working pretty well.
The wrong thing was ,i used serializer.Serializer instead of serializer.ModelSerializer …
But, the thing u have mentioned here will help me in future .tnx a lot

It’s “working”. However, it’s not using Django. You’ve implemented a manual workaround for something Django is already designed to handle for you, which means it’s more code that you need to maintain that doesn’t add anything to the functionality of your system.

so ken from your feedback,
i am thinking that i haven’t become friendly with Django yet.
can you suggest / guide me what can be done to truly understand it.
In short, Am Reza from Bangladesh and have been learning Django implementation in some project with FBV , CBV and DRF for last 7months

Just keep working and moving forward.

Take the time to continue reading the docs. Not like reading a book, but pick different topics at different times to discover what’s in there that you might not be familiar with.

Read the source code of quality projects

Look for high-quality educational resources, such as those listed at Awesome Django

Keep asking questions!

I’ve only been working with Django seriously for about 11 years and I’m still learning…

yea thats true. from start , these 7months whenever got error in console and tried to look solution around here. i found you everytime…
nice to have you helping always.
Btw ken, though am learning from error and stack ,
i have found myself stuck with some situation like built in function and its parameter ,hope you get my question.
and another thing is how you like handle file type things like image ,video or document. Do you prefer them to save directly in database or like we use filefield(upload_to='/media) or use a third party service like cloudinary and store url in database?

No, I’m sorry I don’t.

We don’t rely upon third party services for anything. We manage our own data center.

In most cases, files are stored using the FileField. However, we do have some specific situations where the files being uploaded are considered data and not files, and are stored in the database.