Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'QuerySet' object has no attribute 'x'

I want to go to a users page and see the their photos, so I was trying to get the objects assigned to a foreign key, but I keep getting the error above AttributeError at /user/30/
‘QuerySet’ object has no attribute ‘file’
. I feel like the problem is in my syntax, but I really have no clue why it can’t read my Uploads file model object, but it’s able to read my profile objects.

views.py

def profile_view(request, *args, **kwargs,):
    #users_id = kwargs.get("users_id")
    #img = Uploads.objects.filter(profile = users_id).order_by("-id")
    context = {}
    user_id = kwargs.get("user_id")
    try:
        profile = Profile.objects.get(user=user_id)
        img = profile.uploads_set.all()
    except:
        return HttpResponse("Something went wrong.")
    if profile and img:
        context['id'] = profile.id
        context['user'] = profile.user
        context['email'] = profile.email
        context['profile_picture'] = profile.profile_picture.url
        context['file'] = img.file.url


        return render(request, "main/profile_visit.html", context)

models.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE, null = False, blank = True)
    first_name = models.CharField(max_length = 50, null = True, blank = True)
    last_name = models.CharField(max_length = 50, null = True, blank = True)
    phone = models.CharField(max_length = 50, null = True, blank = True)
    email = models.EmailField(max_length = 50, null = True, blank = True)
    bio = models.TextField(max_length = 300, null = True, blank = True)
    profile_picture = models.ImageField(default = 'default.png', upload_to = "img/%y", null = True, blank = True)
    banner_picture = models.ImageField(default = 'bg_image.png', upload_to = "img/%y", null = True, blank = True)

    def __str__(self):
        return f'{self.user.username} Profile'



class Uploads(models.Model):
    album = models.ForeignKey('Album', on_delete=models.SET_NULL,null=True,blank=True)
    caption = models.CharField(max_length = 100, blank=True, null = True)
    file = models.FileField(upload_to = "img/%y", null = True)
    profile = models.ForeignKey(Profile, on_delete = models.CASCADE, default = None, null = True)
    id = models.AutoField(primary_key = True, null = False)


    def __str__(self):
        return str(self.file) and f"/single_page/{self.id}"

class Album(models.Model):
    name=models.CharField(max_length=400)

>Solution :

img = profile.uploads_set.all() from here img is a queryset.
and file is a field of a upload instance.

you can do the following.

context['file'] = [im.file.url for im in img]

this way you can get all files for a profile.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading