Django-facebook icon indicating copy to clipboard operation
Django-facebook copied to clipboard

user or profile didnt have attribute facebook_id

Open l3ackslash0 opened this issue 10 years ago • 2 comments

Hello,

I'm using django 1.7 on python 3.4 an I'm having this F******* error showing just after every try to login or register on /facebook/example/

I've tried every settingsI found for similar problem on the entire Web but it still doesn't work... is this app still maintained?

There you will find an extract from my UserProfile model (of course I've already specified AUTH_USER_MODEL and AUTH_PROFILE_MODULE):

# UserProfile Custom Class
class UserProfile(FacebookProfileModel):
    #...
    user = models.OneToOneField(User, related_name='user_profile', blank=True, null=True)
    #...

    # Django_Facebook Post Receiver
    @receiver(post_save)
    def create_profile(sender, instance, created, **kwargs):
        """Create a matching profile whenever a user object is created."""
        profile_model = None

        if sender == get_user_model():
            user = instance
            profile_model = get_profile_model()
        if profile_model == get_profile_model() and created:
            profile, new = UserProfile.objects.get_or_create(user=instance)


    post_save.connect(create_profile, sender=User)

#Change auth model to unique email
User._meta.get_field('email')._unique = True

#Autogenerate profile
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

Thank you for the help.

l3ackslash0 avatar Feb 17 '15 23:02 l3ackslash0

It happens because Django 1.7 doesn't have get_profile() method on a user model and Django Facebook relies on it. What you may do is:

  1. Create custom user model as per Django 1.7 instructions and add get_profile() method there. That method should return profile object.

  2. Or go and monkeypatch current user model and add get_profile() method that returns profile object.

timonweb avatar Feb 25 '15 10:02 timonweb

As tim says, 1.7 drops the get_profile method from the User class. If like me you are still using separate user and userprofile classes and switched from .get_profile to just .profile to navigate by orm relation instead of merging the two then you'll get this problem.

The quick fix is to patch the method back in:

from django.contrib.auth.models import User

def get_profile(self):
    return self.profile

User.add_to_class("get_profile", get_profile)

jsvaughan avatar Mar 02 '16 09:03 jsvaughan