MaxenceP
MaxenceP

Reputation: 291

Django: Signal not automatically not creating attribute to User

I have a problem using Djangos Signals while creating a User and a Profile.

I'm trying to create a Profile upon creating a User, but I keep getting the error:

AttributeError at /user/create/
'User' object has no attribute 'profile'

So here is my User Model:

from django.db import models
from django.contrib.auth.models import AbstractUser
from django_countries.fields import CountryField

class User(AbstractUser):
    """auth/login-related fields"""
    is_a = models.BooleanField('a status', default=False)
    is_o = models.BooleanField('o status', default=False)

    def _str_(self):
        return "{} {}".format(self.first_name, self.last_name)

and here is my Profile Model:

from django.db import models
from django_countries.fields import CountryField
from django.contrib.auth import get_user_model
User = get_user_model()
from django.db.models.signals import post_save 
from django.dispatch import receiver



class Profile(models.Model):
    """non-auth-related/cosmetic fields"""
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='Profile')
    birth_date = models.DateTimeField(auto_now=False, auto_now_add=False, null=True)
    nationality = CountryField(null=True)

    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True)

    def __str__(self):
        return f'{self.user.username} Profile'

My User Serializer:

from rest_framework import serializers
from django.contrib.auth import get_user_model
from ..models.model_user import *

class UserIndexSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = [
            'id',
            'username',
            'password',
            'first_name',
            'last_name',
            'email',
            'is_a',
            'is_o'
        ]

class UserCreateSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = [
            'username',
            'password',
            'first_name',
            'last_name',
            'email',
            'is_a',
            'is_o'
        ]
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
        user = User(
            username=validated_data['username'],
            password=validated_data['password'],
            first_name=validated_data['first_name'],
            last_name=validated_data['last_name'],
            email=validated_data['email'],
            is_a=validated_data['is_a'],
            is_o=validated_data['is_o']
            )
        user.set_password(validated_data['password'])
        user.save()
        return user

class UserDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

My signals.py:

from django.db.models.signals import post_save
from django.contrib.auth import get_user_model
User = get_user_model()
from django.dispatch import receiver
from .models.model_profile import *

"""receivers to add a Profile for newly created users"""
@receiver(post_save, sender=User) 
def create_user_profile(sender, instance, created, **kwargs):
  if created:
    Profile.objects.create(user=instance)

@receiver(post_save, sender=User) 
def save_user_profile(sender, instance, **kwargs):
  instance.profile.save()

And when I'm using Postman to post a User:

{
  "username":"16",
  "password":"12345678",
  "first_name":"Al",
  "last_name":"Pongvf",
  "email":"[email protected]",
  "is_a":"False",
  "is_o":"False"
}

It gives me this error message:

AttributeError at /user/create/
'User' object has no attribute 'profile'

I've searched for a solution, but I didn't get lucky:

Does anyone know what am I missing? or doing wrong? Thanks!

Upvotes: 0

Views: 370

Answers (1)

Dean Elliott
Dean Elliott

Reputation: 1323

Your related_name="Profile" on the Proflile model is Profile with a capital. You need to reference it with a capital to use it. I would recommend you rename it to lowercase and make new migrations.

For example:

@receiver(post_save, sender=User) 
def save_user_profile(sender, instance, **kwargs):
  instance.Profile.save()

But really you should change this:

class Profile(models.Model):
    """non-auth-related/cosmetic fields"""
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')

Upvotes: 2

Related Questions