0

I'm using django_facebook. Each time a user registers with fb, her profilpicture is loaded on a field called 'image'

In my models.py I have a field, named profilpic, to allow users to have a profil picture:

        class UserProfile(FacebookProfileModel):
            user = models.OneToOneField(User)
            profilpic = models.ImageField(upload_to="profilepics/", default="profilepics/shadow_user.jpg")


        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)

What I would like to do is when a user registers with facebook, the 'profilpic' takes the value of 'image' (i.e profilpic becomes the facebook picture).

Here is what I tried:

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

        post_save.connect(create_facebook_profile, sender=User)

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

Thank you very much.

JojoL
  • 141
  • 1
  • 8
  • `instance` is a `User` object, not a `UserProfile` object so you need to call `instance.userprofile.save()` instead of `instance.save()` – Timmy O'Mahony Dec 01 '12 at 15:11

1 Answers1

0

You are calling the save method on the wrong instance. If you say instance.save() its only going to call its save method not the userprofile save method.

def create_facebook_profile(sender, instance, created, **kwargs):
             if not created:
                return
             UserProfile.objects.create(user=instance)
             instance.userprofile.profilpic = instance.userprofile.image
             instance.userprofile.save()
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62