[Answered ]-Django: Check that row exists in list

1👍

in your view you could get all user_ids with a profile image, something like:

user_ids_with_profile_images = Profile_images.objects.all().values_list('user_id', flat=True)

Then in your template you could check if profile.user_id not in user_ids_with_profile_images.


It might actually be a little cleaner to loop through all the users with profiles in your system and get their profile images through the foreign key, instead of looping through all the profiles and trying to get the users…

1👍

This is really a design problem, you’ve got a separate model specifically for a profile image when that could just be a field on the profile model itself.

class Profile(models.Model): # convention is to use a non-plural name for models
    # No need to explicitly set the primary key, this will be added automatically
    # profile_id = models.AutoField(primary_key=True) 
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to='uploads/',default = 'uploads/no-img.jpg')
    -----

Now it would just be a case of using {{ profile.image }} with no need for any additional looking up.

👤ptr

Leave a comment