Reputation: 93
#from signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import User, Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
#from models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractUser
from PIL import Image
class User(AbstractUser):
is_paying_customer = models.BooleanField(default=False)
created_at = models.DateTimeField(editable=False)
updated_at = models.DateTimeField()
def __str__(self):
return self.email
def save(self, *args, **kwargs):
if not self.id:
self.created_at = timezone.now()
self.updated_at = timezone.now()
return super(User, self).save(*args, **kwargs)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone_number = models.CharField(max_length=255, blank=True)
address = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=255, blank=True)
country = models.CharField(max_length=255, blank=True)
linked_in = models.CharField(max_length=255, blank=True)
objective = models.TextField(blank=True)
profile_pic = models.ImageField(default="profile-pics/default.jpg/", upload_to="profile-pics")
def __str__(self):
return self.user.email
def save(self):
super().save()
img = Image.open(self.profile_pic.path)
if img.height > 300 or img.width > 300:
output_size = (100, 100)
img.thumbnail(output_size)
img.save(self.profile_pic.path)
#from apps.py
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals
When I create users from Django admin but the profiles never get created with my users. I have included code from various files in my Django project. I believe I have the correct code but maybe a fresh set of eyes will help. What am I doing wrong? Please let me know if you need to see additional code. Thanks in advance!
Upvotes: 1
Views: 2419
Reputation: 49
# settings.py
INSTALLED_APPS = [
...
'users.apps.UsersConfig', # instead of 'users'
...
]
had same problem, this solves for me
Upvotes: 5
Reputation: 93
I was able to fix the issue by adding:
default_app_config = 'users.apps.UsersConfig'
to my users/init.py.
Thanks!
Upvotes: 4