문제 



모델에 MyUser, ActivationProfile, Profile 이렇게 3개 테이블이 있는데 모두 MyUser 모델과 관계를 맺고 있습니다. 

MyUserModel에 User정보가 등록이 되면 그에 따라서 ActivationProfile과, Profile 오프젝트가 생성이 되도록 하는데 왜 외래키를 참조하고 있는 모델들이 오브젝트 생성이 안되는 현상입니다. 


해결 


외래키로 연결을 하고 있는 Pofile 에 email, phone에 'unique=True' 라는 옵션을 제거합니다. 

옵션하나로 ... 문제가 쉽게 해결이 되네요.  


Model.py 

class MyUserManager(BaseUserManager): def create_user(self, username, phone, email, password=None): """ Creates and saves a User with the given email, date of birth and password. """ if not email: raise ValueError('Users must have an email address') user = self.model( username = username, phone = phone, email=self.normalize_email(email), ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, username, phone, email, password): """ Creates and saves a superuser with the given email, date of birth and password. """ user = self.create_user( username, phone, email, password=password, ) user.is_admin = True user.is_staff = True user.save(using=self._db) return user class MyUser(AbstractBaseUser): username = models.CharField(max_length=100, unique=True) email = models.EmailField( verbose_name='email address', max_length=255, unique=True, ) phone = models.CharField(max_length=15, unique=True) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) objects = MyUserManager() USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['phone','email'] def __str__(self): return self.username def has_perm(self, perm, obj=None): "Does the user have a specific permission?" # Simplest possible answer: Yes, always return True def has_module_perms(self, app_label): "Does the user have permissions to view the app `app_label`?" # Simplest possible answer: Yes, always return True class ActivationProfile(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) key = models.CharField(max_length=120) expired = models.BooleanField(default=False) def save(self, *args, **kwargs): self.key = code_generator() super(ActivationProfile, self).save(*args, **kwargs) def post_save_activation_receiver(sender, instance, created, *args, **kwargs): if created: #send Email print('id activation created') url = "/activate" + instance.key post_save.connect(post_save_activation_receiver, sender=ActivationProfile) class Profile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True,) phone = models.CharField(max_length=15, unique=True) # 오류 발생 unique key 삭제 email = models.EmailField(max_length=255, unique=True) # 오류 발생 unique key 삭제 def __str__(self): return str(self.user.username) def post_save_user_model_receiver(sender, instance, created, *args, **kwargs): if created: try: #이곳이 문제 Profile.objects.create(user=instance) ActivationProfile.objects.create(user=instance) except: print(settings.AUTH_USER_MODEL) post_save.connect(post_save_user_model_receiver, sender=settings.AUTH_USER_MODEL)


하하... 이 오류 찾는데 하루...