AbstractUser和AbstractBaseUser在Django之间的区别?

本文探讨了Django中AbstractUser和AbstractBaseUser的区别。AbstractUser是预定义完整用户模型的抽象类,方便扩展,而AbstractBaseUser仅包含认证功能,需要自定义字段。若只需添加额外字段,推荐使用AbstractUser;如需重新设计认证逻辑,则应选择AbstractBaseUser。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用的AbstractUserAbstractBaseUser看起来颇为相似。

 

from django.contrib.auth.models import AbstractUser, AbstractBaseUser

从django.contrib.auth.models导入AbstractUser,AbstractBaseUser

2.What is the difference between the two?

The documentation explains this fully. AbstractUser is a full User model, complete with fields, as an abstract class so that you can inherit from it and add your own profile fields and methods. AbstractBaseUser only contains the authentication functionality, but no actual fields: you have to supply them when you subclass.

文档充分解释了这一点。 AbstractUser是一个完整的用户模型,包含字段,作为一个抽象类,以便您可以继承它并添加您自己的配置文件字段和方法。 AbstractBaseUser仅包含身份验证功能,但不包含实际字段:当您继承子类时,您必须提供它们。

The AbstractUser is basically just the "User" class you're probably already used to. AbstractBaseUser makes fewer assumptions and you have to tell it what field represents the username, what fields are required, and how to manage those users.

AbstractUser基本上就是您可能已经习惯的“用户”类。 AbstractBaseUser的继承较少,您必须告诉它哪个字段代表用户名,需要哪些字段以及如何管理这些用户。

If you're just adding things to the existing user (i.e. profile data with extra fields), then use AbstractUser because it's simpler and easier. If you want to rethink some of Django's assumptions about authentication, then AbstractBaseUser gives you the power to do so.

如果您只是将事情添加到现有用户(即具有额外字段的配置文件数据),则使用AbstractUser是因为它更简单,更简单。 如果您想重新考虑一下Django关于认证的假设,那么AbstractBaseUser会为您提供这样的权力。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Christian_yang

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值