0

[Django 1.5.1]

I've set up django-profiles and django-registration for my small site in a way that involves a 'custom' registration backend such that during registration, a new user fills out their username, password, and profile fields in one go. But I'm now trying to allow users to log in with Facebook and trying to use django-facebook. As far as I understand, logging in through django-facebook's Facebook login will authenticate someone, but that won't create a user object nor a profile object.

So the idea I had in mind would be to add an extra profile field which holds a user's potential Facebook ID(which I believe is unique) if they want to use Facebook. Then force a check on every template if the user has a profile or not, and if not, to direct them to the 'create profile' page. Then whenever a user logs in through the Facebook login, it'll somehow link their session with the corresponding profile object which matches the Facebook ID (and consequently the user object corresponding to the profile object). I think I'd have to apply a filter and then create a 'signal', but I'm not too sure how to do that.

This sounds very convoluted though. How might I be able to get this accomplished the right way?

Stylex
  • 73
  • 8

1 Answers1

0

Here's how I suggest you do things. Do away with django-facebook and look into django-allauth. It will handle accounts (registration, logic, connecting social accounts).

Also, django-profiles has many issues with 1.5+ for me and I don't bother with it and instead create my own profiles app and UserProfile model with any additional fields that wouldn't be handled by django-allauth.

An example from one of my implementations

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    default_address = models.OneToOneField(Address, blank=True, null=True)
    default_tshirt_size = models.CharField(blank=True, null=True, choices=constants.tshirt_sizes, max_length=50)
    default_shoe_size = models.CharField(blank=True, null=True, choices=constants.shoe_sizes, max_length=50)

Your user profile model should be pointing to the same User model allauth is using.

from allauth.utils import get_user_model
User = get_user_model()

Here's a neat method I use to create the User's profile automatically if it hasn't been already.

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

Then just create a sign-up form class with a save() method that takes user as an argument. Link to this class in your settings file.

ACCOUNT_SIGNUP_FORM_CLASS = 'yourproject.yourapp.forms.SignupForm'

See this post on for a somewhat relevant example, except of course with your implementation you'll probably want to get the UserProfile based on the user.id at that point, and save the additional values to the profile instance.

How to customize user profile when using django-allauth

Community
  • 1
  • 1
Kendrick Ledet
  • 673
  • 6
  • 9
  • Thanks so much for the reply! I guess I could get looking into django-allauth instead, but my site already has a few existing users and I've already set up my custom userprofiles class along with django-profiles and django-registration...Is there still some non-messy way for me to integrate Facebook with django-allauth? Thanks again! – Stylex Jul 15 '13 at 03:00