'Foreignkey relationship returning an AttributeError when queried in Django

Hy please am new to Django,I have a childmodel(relationship) referencing a parent model(Profile) through a foreign key. I want to get the relationship status of a particular profile, like querying it from backwards. Here's the code.

from django.db import models 
from django.contrib.auth.models import User 

#Create your models here.

class Profile(models.Model): 
    user = models.OneToOneField (User, on_delete= models.CASCADE) 
    prof_pics = models.ImageField (null = True, blank = True, upload_to = 'images/') 
    friends = models.ManyToManyField (User, blank=True, related_name="friend" ) 
    bio = models.TextField(blank=True)

def__str__(self): 
    return str(self.user) 

STATUS CHOICES = (

("accept", "accept"), 
("send","send"),
)

def ___str__(self):
    return str(self.user)

class Relationship(models.Model):
    sender = models.Foreignkey(Profile, on_delete = models.CASCADE, null=True, related_name = "senders") 
    date_created = models.DateTimeField(auto_now_add= True) 
    receiver = models.ForeignKey(Profile, on_delete= models.CASCADE, null= True, related_name= 'receivers') 
    status = models.Charfield(max_length=10, choices= STATUS_CHOICES) 

def_str_(self): 
    return f"{self.sender}-{self.receiver}-{self.status}"

The query I ran in my view to get the relationship of a particular profile as I saw a tutorial that did same thing with similar models.

#imported necessary dependencies

def relationship_view(request):
    idd = request.user.id
    profiles =Profile.objects.get(id=idd)
    rel=profiles.relationship_set.all()
    Print(rel)
    return render(request, "profiles/relationship_query.html", {})

A screenshot from the tutorial

The error I get when I run my own view

File "C:\Users\semper\djangotry\twitterclone\profiles\views.py", line 96, in Relationship_view 
  rel = profiles.relationship_set.all()
AttributeError: 'Profile object has no attribute 'relationship_set" 


Solution 1:[1]

You set the related_name fields as senders and receivers in Relationship model, so you need to use those.

def relationship_view(request):
    idd = request.user.id
    profile = Profile.objects.get(id=idd)

    # you can get receivers from you and get senders to you.
    receiver_relationship = profile.senders.all()
    sender_relationship = profile.receivers.all()

    ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Lu