Home > front end >  'QuerySet' object has no attribute 'x'
'QuerySet' object has no attribute 'x'

Time:02-25

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

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)

CodePudding user response:

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.

CodePudding user response:

This:

img = profile.uploads_set.all()

is a queryset, so it doesn't have an attribute file.

You can iterate over it, and its individual members will have a file attribute.

url_list = []
for i in img:
    url_list.append(i.file.url)

will then give you a list of the URLs you want.

You could also do it as a list comprehension:

url_list = [i.file.url for i in img]
  • Related