0

I'm using django-facebook and I would like the profile-picture to be the picture of my user when they register with facebook.

In my custom User model (Userprofile) I have a

         profilpic = models.ImageField(...)

I also have this function in my models.py:

   def create_facebook_profile(sender, instance, created, **kwargs):
      if not created:
          return
      UserProfile.objects.create(user=instance)

   post_save.connect(create_facebook_profile, sender=User)

I was wondering if it's possible to use the facebook profile picture as the 'profilpic' of my user.

I tried to do that:

     def create_facebook_profile(sender, instance, created, **kwargs):
      if not created:
          return
      UserProfile.objects.create(user=instance)

      user_data = json.loads(instance.raw_data)          
      user_pic = user_data.get('image')
      user = request.user
      user.userprofile.profilpic = user_pic
      user.save()

   post_save.connect(create_facebook_profile, sender=User)

But it doesn't work. Any idea on how to do that?

Zulu
  • 8,765
  • 9
  • 49
  • 56
Juliette Dupuis
  • 1,027
  • 2
  • 13
  • 22

1 Answers1

0

You're trying to save the URL to an ImageField, you need to actually read and save the image from Facebook locally first and then assign that new file to the ImageField (assuming that the json actually has an image url). Have a look at this similar question:

Django: add image in an ImageField from image url

Community
  • 1
  • 1
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177