在模型中保存数据时,user.id错误-提交False不起作用

时间:2019-04-05 07:43:29

标签: django django-models

我正在使用AbstractBaseUser,因为我已经创建了managers.py用于创建superuser

一切工作正常,但manager.py中的这些行除外。甚至我在print(user.id)之前都user.save(using=self._db)正在打印None

user.save(using=self._db)
user.created_by_id = user.id
user.updated_by_id = user.id
user.save()

我在commit=False中也使用过user.save(using=self._db, commit=False),但它仍给出错误。查看底部的错误。

在我的models.py中,我有tweo字段created_byupdated_by,不应为null。那就是为什么我要保存在创建所有此错误的Managers.py中。

我该如何解决此问题。有没有更好的方法。仅创建超级用户才需要此条件。

Models.py

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_active = models.BooleanField(_('is user active'), default=True)
    profile_image = models.ImageField(default='user-avatar.png', upload_to='users/', null=True, blank=True)
    is_staff = models.BooleanField(_('staff'), default=True)

    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    phone_number = models.CharField(max_length=30, blank=True, null=True)
    address = models.CharField(max_length=100, blank=True, null=True)

    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, related_name='user_created_by')
    created_on = models.DateTimeField(_('user created on'), auto_now_add=True)
    updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, related_name='user_updated_by')
    updated_on = models.DateTimeField(_('user updated on'), auto_now=True)

Managers.py

class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.role_id = 1
        user.first_name = "Super"
        user.last_name = "Admin"
        user.save(using=self._db)
        user.created_by_id = user.id
        user.updated_by_id = user.id
        user.save()
        return user

    def create_user(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)

错误-commit=False

super().save(*args, **kwargs)
TypeError: save() got an unexpected keyword argument 'commit'

错误-没有提交错误

raise utils.IntegrityError(*tuple(e.args))
django.db.utils.IntegrityError: (1048, "Column 'created_by_id' cannot be null")

1 个答案:

答案 0 :(得分:1)

commit=False适用于ModelForms。它不适用于模型实例。就像您的问题中所说的错误一样,save方法中不存在名为commit的关键字参数。

此外,在保存用户对象之前,它不能产生任何主键。我建议使created_by为空。这样可以避免完整性错误。

created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, related_name='user_created_by', null=True, default=None)

updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, related_name='user_updated_by', null=True, default=None)

仍然,如果要强制执行此限制,则可以在表单或序列化器中执行此操作,也可以在User模型中覆盖save方法。

例如:

class User(...):
   ...
   def save(self, *args, **kwargs):
       if not self.is_superuser:
          if not self.created_by or not self.updated_by:
               raise Exception("Add these fields")  # can be custom exception

       super(User, self).save(*args, **kwargs)