0

I' m using django-allauth to authenticate users from different social sites. When a User is created in my db, i' d like to call a function which would create a UserProfile (or would do something) and to achieve this the best would be to use signals.post_save .

I' ve multiple django applications and of course i don' t want to change the core django-allauth, so my question where do i place my post_save code?

Thanks.

user2194805
  • 1,201
  • 1
  • 17
  • 35

2 Answers2

2

You could create an accounts app locally to hold the UserProfile and everything associated with it. The you could have:

# Connecting to AllAuth Signals
from allauth.account.signals import user_signed_up
from django.dispatch import receiver

@receiver(user_signed_up)
def new_user_signup(sender, **kwargs):
    p = UserProfile(user = kwargs['user'])
    p.save()
zaphod
  • 2,045
  • 1
  • 14
  • 18
  • Ah, ok. I had the same code but the location was wrong. Basically i can put in any "own application", but instead of signal(s).py, i' ve to put it to the ``__init__.py'', right? Or least that' s the one working for me. Where would You put your code? What' s the best place for it? – user2194805 Dec 18 '13 at 11:26
  • It depends. `__init__.py` may be ok. I had also added extra fields to my registration form, so I kept it close to the code doing that form processing. – zaphod Dec 19 '13 at 21:59
  • 1
    This worked for. I actually had an issue where i was using a signal for regular sign ups and Facebook wasn't creating a Profile object. I was duplicating the process. I then added this signal by itself and it did both for me. – JDavies Dec 15 '16 at 09:59
0

In my experience "user_signed_up" signal from allauth and "post_save" from django didn't worked well together. I have email confirmation for original signup process but not for alluth.

So I made a different approach. After a successfull login, all my users redirect to homepage. I made a "get_or_create" method here. It's basically doing same thing as "post_save" and "user_signed_up". With this method I don't need to worry about email confirmations too.

Here is my views code:

from django.views.generic import TemplateView

from .models import UserProfile



class HomePageView(TemplateView):
    template_name = 'front/homepage.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        user = self.request.user
        if user.is_authenticated: 
            p, created = UserProfile.objects.get_or_create(user=user)
            context['balance'] = p.balance
        return context

My models:

from django.db import models
from django.contrib.auth.models import User



class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="user_profile")  
    balance = models.DecimalField(default=0.00, max_digits=9, decimal_places=2)
          

    class Meta:
        verbose_name = "User Card"
        verbose_name_plural = "User Cards"

    def __str__(self):
        return self.user.username
Tech
  • 734
  • 1
  • 6
  • 20