Exactman
Exactman

Reputation: 45

Cannot resolve keyword 'username' into field. Choices are: bio, blog, describtion, id, image, speciality, status, user, user_id

hey i want to create a profile page for my user,in which when people logging to the website they can view the profile of every user,i get the above error anytime i tried to log on to every profile of the user i get it, this is my code below

views.py

class DoctorDetailView(LoginRequiredMixin, DetailView):
    model = Doctor
    fields = ['user', 'email', 'image', 'speciality', 'bio']
    template_name = 'pages/doctor_detail.html'

    def get_queryset(self):
        user = get_object_or_404(Doctor, username=self.kwargs.get('username'))
        return Doctor.objects.filter(doctor=user.doctor)

urls.py

    path('doctor/', doctor, name='doctor'),
    path('doctor/info/<str:username>', user_views.DoctorDetailView.as_view(), name='doctor-detail'),

doctor.html

<a href="{% url 'doctor-detail' doc.user.username %}"><div class="img-wrap d-flex align-items-stretch">
                                <div class="img align-self-stretch" style="background-image: url({{ doc.user.doctor.image.url }}"></div>

models.py

class CustomUser(AbstractUser):
    is_doctor = models.BooleanField(default=False)

    def __str__(self):
        return self.email


class Status(models.Model):
    title= models.CharField(max_length=5)


    def __str__(self):
        return self.title


class Doctor(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, null=True, related_name="doctor")
    image = models.ImageField(default='jazeera.jpg', upload_to='profile_pics')
    bio = models.TextField()
    speciality = models.CharField(max_length=300)
    describtion = models.CharField(max_length=100)
    status = models.ManyToManyField(Status)

Upvotes: 1

Views: 205

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

A Doctor object has no username, hence:

user = get_object_or_404(Doctor, username=self.kwargs.get('username'))

makes not much sense, you however do not need to use get_object_or_404 to fetch the user first, you can filter with:

from django.shortcuts import get_object_or_404

class DoctorDetailView(LoginRequiredMixin, DetailView):
    model = Doctor
    fields = ['user', 'email', 'image', 'speciality', 'bio']
    template_name = 'pages/doctor_detail.html'

    def get_object(self):
        return get_object_or_404(Doctor, user__username=self.kwargs['username'])

Upvotes: 1

Related Questions