Search code examples
djangofacebookfacebook-canvasfandjango

Connect fandjango (Facebook django) user to Django User to add attributes


I'm new to FanDjango, and trying to understand something. I've got everything set up and working beautifully, and I can pole request.facebook.user and get info.

But now, I want to develop an application that has more info than just Facebook gives me about each user. For example, I want to store up/down votes this user has mode on other objects (i.e. relations). I want to store user settings. Etc... The standard "Django Way" is to use a User Profile, but I don't know how to connect FanDjango's user to the user profile.

I'm unclear how to do this.

I looked into django-facebook before fandjango, and I couldn't get it to work which is why I moved to this. What they do is, they allow your user-profile to inherit from their user definition. Can the same kind of thing work here, or should I be doing something different?

Thanks


Solution

  • Well you can simply create a Profile model and give it a OneToOneField pointing to the Fandjango profile

    class Profile(models.Model):
         facebook_user = models.OneToOneField("fandjango.User")
         votes = ...
    

    which will allow you to get the Fandjango profile given the Profile and visa versa:

    request.facebook.user.profile
    

    and in reverse:

    profile.facebook_user 
    

    An issue with this is that because you aren't using django.contrib.auth at all, you don't get any of it's nice features such as being able to use {% if user.is_authenticated %} etc.

    EDITED TO ADD:

    Extra details provided here: https://github.com/jgorset/fandjango/issues/63#issuecomment-4988917

    Then I just listen for the post_save signal of "Fandjango.models.User" and when the "created" parameter is set, I instantiate the Profile model.

    @receiver(post_save, sender=User)
    def profile_creation(sender, instance, created, **kwargs):
        if created:
            profile, created = Profile.objects.get_or_create(user=instance)