RelatedObjectDoesNotExist User has not profile

1

I'm trying to create a profile model for the general user that Django has by default, but it seems that when creating a user from manage.py I get the error that the title has along with this one:

django.db.utils.IntegrityError: NOT NULL constraint failed: userprofiles_profile.birth_date

This is the models.py of the Profile application

from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver


# Create your models here.
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(blank=True)
    website = models.URLField(blank=True)
    birth_date = models.DateField(blank=True, default=None)

    def __str__(self):
        return self.user.username

@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()

I do not know if the signals have anything to do with the inconvenience I'm presenting and I do not know if they need any other part of the code to help me solve this problem.

Thanks in advance to who can help me.

    
asked by ikenshu 25.08.2017 в 00:32
source

1 answer

2

Your fields bio , website and birth_date are NOT NULL , Django creates it this way by default if you do not indicate it explicitly.

Modify your model so that these fields accept null values since at the time of creating the profile with the signal you do not have these values at hand:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(null=True, blank=True)
    website = models.URLField(null=True, blank=True)
    birth_date = models.DateField(null=True, blank=True, default=None)

You will have to run a migration for the changes to take effect.

    
answered by 25.08.2017 / 02:02
source