Nodirbek
Nodirbek

Reputation: 49

Cannot get data of logged in User in Django

Respect for everyone here. I have CustomUser model from one app, and Field model from another app. And I put a connection in CustomUser model with Field model via ManyToMany.

Field is for interested fields, and in signup form I have input like "In What do you have interests?"

I can easily get other datas like username, date_of_birth, email... But cannot get this interests. It returns courses.Field.None

Here is the models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from courses.models import Field

class CustomUser(AbstractUser):
    username = models.CharField(max_length=32 , verbose_name="Login", unique=True, )
    first_name = models.CharField(max_length=64, verbose_name="Ismingiz", blank=False, null=False)
    second_name = models.CharField(max_length=64, verbose_name="Familyangiz", blank=False, null=False)
    # & dob = date of birth
    dob = models.DateField(auto_now_add=False, verbose_name="Yoshingiz", blank=False, null=True)
    gender_choices = [("e", "Erkak"), ("a", "Ayol")]
    gender = models.CharField(choices=gender_choices, verbose_name="Jinsingiz", default="e", max_length=1)
->  interests = models.ManyToManyField(Field, verbose_name="Qiziqishlaringiz")
    longitude = models.CharField(max_length=256, verbose_name="Yashash joyingiz (uzunlik)", null=True, blank=True)
    latitude = models.CharField(max_length=256, verbose_name="Yashash joyingiz (kenglik)", null=True, blank=True)

And this is my views.py file from courses App

from django.shortcuts import render
from .models import Center
from .serializers import CenterSerializer, UserSerializer
from rest_framework import generics
from django.contrib.auth import get_user_model
from rest_framework.response import Response
from decimal import Decimal
from slugify import slugify
import operator
from rest_framework.decorators import api_view
...
other views here
...
def center_list(request):
    if request.method == 'GET':
        dorilar = Center.objects.all()
        serializers = CenterSerializer(dorilar, many=True)
        dorixonalar = []
        uzunlik = request.GET.get('uz')
        kenglik = request.GET.get('keng')
------> print(request.user.interests)
        interests = request.GET.get('interest').split(",")
        if uzunlik and kenglik:
            uzunlik = Decimal(uzunlik)
            kenglik = Decimal(kenglik)
            location = Decimal(uzunlik + kenglik)
            dorixonalar = yaqinlik_boyicha_filter(dorilar, location, interests)
            serializers = CenterSerializer(dorixonalar, many=True)
            return Response(serializers.data)
        else:
            return Response(serializers.data)

Thanks in advance. :)

Upvotes: 0

Views: 137

Answers (1)

lucutzu33
lucutzu33

Reputation: 3700

request.user.interests is a Manager. You need to:

print(request.user.interests.all())

Upvotes: 2

Related Questions