1👍
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
else:
maker['profileImg'] = ''
For one liner:
maker['profileImg'] = item.user.profile_pic.url if item.user.profile_pic else ''
0👍
If you want to avoid the None
value, you could use this in one-line:
profileImg = item.user.profile_pic.url or ''
EDIT
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
- Restrict urls on certain apps in Django
- Fastest way to retrieve newly registered users' instances in Django web app
Source:stackexchange.com