post_save signal is not dispatched

6 views
Skip to first unread message

Dejan Spasić

unread,
Jun 24, 2018, 5:01:45 PM6/24/18
to Django users
I don't wont to bother you with a long text and will first show you the code. I think this should be self explanatory. If not, let me know :)

#####################
from typing import Any, Dict

from django.apps import AppConfig
from django.db.models.signals import post_save

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


class EmployerConfig(AppConfig):
name = "employer"
verbose_name = "Employer"

def ready(self) -> None:
def post_save_user(**kwargs: Dict[str, Any]) -> None:
if kwargs['created']:
return None

user: User = kwargs['instance']

if user.employer:
return None

employer = Employer(user=user)
employer.save()

post_save.connect(post_save_user, User) #####################

As you can see I want to create an employer and link them with the new created user. 

Running the command

./manager.py createsuperuser --username username --email us...@user.com ***********

shows me that I'm wrong. There is no entry in the myapp_employer table. What did I missend?

* Django: 2.0.X 
* app is installed 
* python: 3.6

Dan Tagg

unread,
Jun 24, 2018, 5:20:10 PM6/24/18
to django...@googlegroups.com
Hi,

Weirdly, I've just been doing this. What do you have in your settings INSTALLED_APPS? 

One of them should be something like 'employer.apps.EmployerConfig' alternatively in your employer module's __init__.py file you need to say 
default_app_config = 'employer.apps.EmployerConfig'

Dan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8d0c4241-3d1a-46c9-9e52-6cc4f46a3906%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Wildman and Herring Limited, Registered Office: 28 Brock Street, Bath, United Kingdom, BA1 2LN, Company no: 05766374

Melvyn Sopacua

unread,
Jun 24, 2018, 5:26:32 PM6/24/18
to django...@googlegroups.com
On zondag 24 juni 2018 15:45:47 CEST Dejan Spasić wrote:
> def post_save_user(**kwargs: Dict[str, Any]) -> None:
> if kwargs['created']:
> return None

So for new users, you bail out and do nothing. Your code works exactly as you
wrote it.

--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages