[Fixed]-The 'profile_pic' attribute has no file associated with it


Just use if condition to check if there is an image or not in that particular field,

if item.user.profile_pic:
    maker['profileImg'] = item.user.profile_pic.url
    maker['profileImg'] = ''

For one liner:

maker['profileImg'] = item.user.profile_pic.url if item.user.profile_pic else ''


If you want to avoid the None value, you could use this in one-line:

profileImg = item.user.profile_pic.url or ''


Do not store url, store profile_pic instead:

profileImg = item.user.profile_pic

And the when you need url:

if profileImg:
    print profileImg.url  # or whatever you need to di with url

Leave a comment