1

I am trying to keep track of a user avatar for all of my users. I have added an imageField to my user model. I also have a facebook login and if a user logs in with facebook I am happy just using their profile pic as the avatar and letting facebook host this, but I would also like users that don't login with facebook to upload an avatar. The problem is that when I try to save directly to the ImageField like this:

model:

avatar = models.ImageField(upload_to='avatars/%Y/%m/%d', default="", blank=True)

in view:

info.avatar = 'http://graph.facebook.com/555555/picture/' 

then it saves the url relative to the media url so when I try to load it I get this for the avatar.url:

http://domain.com/media/https%3A//graph.facebook.com/555555/picture/

Which is obviously not what I want. Is there any way to save them both in the same field or am I forced to create two separate fields, a profile_pic_url field and an avatar field?

Chase Roberts
  • 9,082
  • 13
  • 73
  • 131

1 Answers1

0

If you want to save the image, there's no need to have two fields (because you use the url to fetch the image and there's no need to save the url too). If you want to see how to do it, you can use django-social-auth and this answer for reference.

But, wait a minute - is that really what you want? Think about it, what if a user changes his profile image? Wouldn't you also the image on your website to change as well? In that case, use the generic path to the user's profile picture, which (if I'm not mistaken) normally would be http://graph.facebook.com/<user_id>/picture

edit

A good idea would be to do it like this:

class Info(models.Model):
    #... all the fields
    image = models.URLField() #or a charField. That's what this is, basically

    def save(self, *args, **kwargs):
        if (connected with facebook):
            self.image = 'http://graph.facebook.com/555555/picture/'
        else:
            self.image = 'http://domain.com/media/avatars/someimage.jpg'

        super(Info, self).save(*args, **kwargs)

Now your model has only one field, and you can customize your modelform to include a non-required file-field. Does that make sense to you?

Community
  • 1
  • 1
yuvi
  • 18,155
  • 8
  • 56
  • 93
  • I don't want to save the image. I would like to use the url to their facebook picture. But if they haven't logged in with facebook I want them to be able to upload an avatar. But I would like that link to be saved in one place, whether it is referencing an image they uploaded to the media directory or whether it is hosted on facebook. – Chase Roberts Feb 12 '14 at 17:22
  • Well, when you upload a file it is saved somewhere and what you're left off with is a link to that image. So technically, you *can* use only one field for this, but you'll have to add your own validation, the easiest way probably by overriding the `save` method – yuvi Feb 12 '14 at 17:28
  • See my answer for suggested solution. Just because you want them to be able to upload an image, doesn't require you to have an ImageField – yuvi Feb 12 '14 at 17:33
  • This combined with the answer here: http://stackoverflow.com/a/3488552/1481072 did it for me. Thanks. – Chase Roberts Feb 12 '14 at 21:13