DJANGO:当使用信号在“其他”应用程序中创建联系人时,在“核心”应用程序中创建用户

问题描述 投票:0回答:1

我想在名为“预订”的应用程序中创建联系人时在名为“核心”的单独应用程序中创建用户。 我在配置信号来处理这个问题时遇到问题。

目前,我正在使用信号检查用户是否存在,并将现有用户分配给联系人实例或创建新用户并分配。

我已经尝试将用户属性设置为允许 null 和空白等。我不断收到以下警告:

IntegrityError 在/bookings/contacts/ 关系“bookings_contact”的“user_id”列中的空值违反了非空约束 详细信息:失败行包含 (18, [email protected], 5555555, null)。

bookings.models.py

from django.db import models
from django.conf import settings
from django.contrib import admin
import datetime

# Create your models here.

class Contact(models.Model):
    email = models.EmailField(max_length=255, null=True, blank=True)
    phone = models.CharField(max_length=255, null=False, blank=True)
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=False, blank=True)

核心.models.py

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

"""
Creating a User in Core app to decuple the core app from the bookings app.
Email UNIQUE constraint handled in serializers.py and admin.py
"""

class User(AbstractUser):
    email = models.EmailField(unique=True)

core.signals.handlers.py

from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.crypto import get_random_string

from bookings.models import Contact
from core.models import User

@receiver(post_save, sender=Contact)
def create_user_for_new_contact(sender, instance, created, **kwargs):
    if created and instance.email:
        email = instance.email
        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            password = get_random_string(length=12)
            user = User.objects.create(email=email, password=password)
        instance.user = user
        instance.save()

核心.apps.py

from django.apps import AppConfig


class CoreConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'core'

    def ready(self) -> None:
        import core.signals.handlers
django django-users django-signals
1个回答
0
投票

bookings.models.py 中的联系人模型:

class Contact(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255, null=False)
    email = models.EmailField(max_length=255, null=True, unique=True)
    phone = models.CharField(max_length=255, null=False, blank=True)
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)

迁移文件:

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('bookings', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='contact',
            name='email',
            field=models.EmailField(max_length=255, null=True, unique=True),
        ),
    ]

运行'makemigrations'时的终端消息: ←[1mbookings\migrations _alter_contact_email.py←[0m - 更改联系人字段电子邮件

core.models.py 中的用户模型:

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

"""
Creating a User in Core app to decuple the core app from the bookings app.
Email UNIQUE constraint handled in serializers.py and admin.py
"""

class User(AbstractUser):
    email = models.EmailField(unique=True)

好像没有取first_name和last_name?

© www.soinside.com 2019 - 2024. All rights reserved.