Django从1.10升级到1.11的DeprecationWarning

21 浏览
0 Comments

Django从1.10升级到1.11的DeprecationWarning

我正在尝试将我的项目从Django 1.10升级到1.11:\n

>>> django.VERSION
(1, 10, 0, 'final', 1)

\n当我运行python -Wall manage.py test时,我收到以下错误信息:\n

C:\Users\Environments\lib\site-packages\django\contrib\auth\base_user.py:52: DeprecationWarning: __class__ not set defining 'AbstractBaseUser' as . Was __classcell__ propagated to type.__new__? class AbstractBaseUser(models.Model):

\n我已经查找了一些解决方案,但没有找到解决这个问题的方法。鉴于我正在扩展用户类,这应该不仅仅是针对我的项目。所以,有人找到了解决办法吗?\n

class User(AbstractBaseUser, PermissionsMixin):
    """
    自定义用户类
    """
    USER_TYPE = (
        ('owner', 'Owner'),
        ('developer', 'Developer'),
        ('general contractor', 'General Contractor'),
        ('general contractor\'s employee', 'General Contractor\'s Employee'),
        ('consultant', 'Consultant'),
        ('subcontractor', 'Subcontractor'),
        ('home owners', 'Home Owners'),
        ('construction financier', 'Construction Financier'),
        ('lawyer', 'Lawyer'),
        ('accountant', 'Accountant'),
        )
    email = models.EmailField(verbose_name = 'email address',unique = True, db_index = True)
    # email是用于标识目的的唯一字段
    joined = models.DateTimeField(auto_now_add = True)
    is_active = models.BooleanField(default = True)
    is_admin = models.BooleanField(default = False)
    is_superuser = models.BooleanField(default = False)
    #is_staff = models.BooleanField(default = False)
    user_type = models.CharField(max_length = 30, choices = USER_TYPE)
    group = models.ManyToManyField(Group, related_name = 'users')
    permission = models.ManyToManyField(Permission, related_name = 'users')
    objects = CustomUserManager()
    # Added:
    #company = models.ForeignKey(Company, related_name = 'users')
    USERNAME_FIELD = 'email'  # 唯一标识符(必填)该字段在其定义中必须设置unique=True(见上文)
    def get_full_name(self):
        return self.email
    def get_short_name(self):
        return self.email
    def has_perm(self, perm, obj=None):
        ''' 用户是否具有特定权限 '''
        return True   # 这可能需要根据我们想要查找权限的对象进行修改
    def has_module_perms(self, app_label):
        ''' 用户是否有权限查看应用程序'app_label'?默认答案是是。
        这可能会在以后进行修改。 '''
        return True
    @property
    def is_staff(self):
        ''' 用户是否是员工的成员? '''
        return self.is_admin
    def __unicode__(self):
        return '{user_email}, {user_title} joined on {joined_date}'.format(user_email = self.email,
                                                                           user_title = self.user_type,
                                                                           joined_date = self.joined)
    def __str__(self):
        return '{user_email}, {user_title} joined on {joined_date}'.format(user_email = self.email,
                                                                           user_title = self.user_type,
                                                                           joined_date = self.joined)   

0
0 Comments

这个问题的出现是因为在Python 3.6中,Django存在一个问题。这个问题在Python 3.5中并不存在。如果将Python版本从3.6.x降级到3.5.x,这个问题应该会消失。根据您的帖子,目前还不清楚这个问题是否在运行时引起了实际问题,或者是否只是一个错误触发的警告。

以下是一些解决这个问题的方法:

1. 将Python版本降级到3.5.x。这是最简单的解决方法,因为这个问题只存在于Python 3.6中。通过降级Python版本,可以避免这个问题。

2. 查看Django的官方文档和错误日志,以获取关于这个问题的更多信息和解决方法。Django的官方文档通常会提供关于版本升级和兼容性问题的说明。错误日志可能会提供有关这个问题的更具体的信息,帮助您更好地理解和解决它。

3. 检查您的代码中是否有使用了被弃用的Django功能或API。Django升级过程中,有些功能可能已经被弃用或改变了用法。通过查找和更新这些被弃用的功能,可以避免出现这个警告。

4. 参考其他开发者在类似问题上的解决方案。在Stack Overflow等开发者社区中,可能已经有其他开发者遇到并解决了类似的问题。搜索相关问题,并查看其他开发者的解决方案,可能会帮助您找到解决这个问题的方法。

总结起来,Django在Python 3.6中出现了一个问题,可以通过降级Python版本、查看官方文档和错误日志、更新代码中的被弃用功能或API,以及参考其他开发者的解决方案来解决这个问题。

0