django,save() 缺少 1 个必需的位置参数“请求”
Posted
技术标签:
【中文标题】django,save() 缺少 1 个必需的位置参数“请求”【英文标题】:django, save() missing 1 required positional argument 'request' 【发布时间】:2018-11-18 17:44:37 【问题描述】:我尝试使用 create_user 作为自定义用户模型进行电子邮件登录,但它不起作用。 save() 缺少 1 个必需的位置参数“请求”。 怎么解决这个问题,请帮帮我。
这是回溯..
文件“/Users/maxx/.local/share/virtualenvs/testauth--sM0sqjl/lib/python3.6/site-packages/rest_framework/views.py”,第 480 行,在调度中 响应 = 处理程序(请求,*args,**kwargs) 文件“/Users/maxx/Documents/workspace/python3/testauth/users/views.py”,第 12 行,在帖子中 用户 = 序列化器.save() 类型错误:save() 缺少 1 个必需的位置参数:'request'
models.py
class UserManger(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
if not email:
raise ValueError('You must give email address')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
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_staff', True)
extra_fields.setdefault('is_superuser', True)
return self._create_user(email, password, **extra_fields)
class User(AbstractBaseUser):
email = models.EmailField(unique=True)
nick_name = models.CharField(max_length=50)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManger()
def __str__(self):
return self.email
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):
return True
序列化器.py
class RegisterSerializer(serializers.Serializer):
email = serializers.EmailField(required=allauth_settings.EMAIL_REQUIRED)
password1 = serializers.CharField(write_only=True)
password2 = serializers.CharField(write_only=True)
def validate_email(self, email):
email = get_adapter().clean_email(email)
if allauth_settings.UNIQUE_EMAIL:
if email and email_address_exists(email):
raise serializers.ValidationError(
_("A user is already registered with this e-mail address."))
return email
def validate_password1(self, password):
return get_adapter().clean_password(password)
def validate(self, data):
if data['password1'] != data['password2']:
raise serializers.ValidationError(_("password fields didn't match."))
return data
def get_cleaned_data(self):
return
'email': self.validated_data.get('email', ''),
'password1': self.validated_data.get('password1', ''),
def save(self, request):
adapter = get_adapter()
user = adapter.new_user(request)
self.cleaned_data = self.get_cleaned_data()
adapter.save_user(request, user, self)
setup_user_email(request, user, [])
return user
views.py
class UserCreateView(APIView):
def post(self, request):
serializer = RegisterSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save()
if user:
token = Token.objects.create(user=user)
json = serializer.data
json['token'] = token.key
return Response(json, status=status.HTTP_201_CREATED)
return Response(data=serializer.errors ,status=status.HTTP_400_BAD_REQUEST)
【问题讨论】:
【参考方案1】:在您的RegisterSerializer
中,您实现了一个save()
方法,该方法需要一个名为request
的位置参数,在UserCreateView
中,您需要在该方法调用中包含该请求。
class UserCreateView(APIView):
def post(self, request):
serializer = RegisterSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save(request) # <---- INCLUDE REQUEST
if user:
token = Token.objects.create(user=user)
json = serializer.data
json['token'] = token.key
return Response(json, status=status.HTTP_201_CREATED)
return Response(data=serializer.errors ,status=status.HTTP_400_BAD_REQUEST)
【讨论】:
以上是关于django,save() 缺少 1 个必需的位置参数“请求”的主要内容,如果未能解决你的问题,请参考以下文章
Django:“缺少1个必需的位置参数:'lookup _name'”,当它没有丢失时?
Django - PUT 端点验证器错误“wrapped_view() 缺少 1 个必需的位置参数:'request'”
Django - get_queryset() 缺少 1 个必需的位置参数:“请求”
文件上传 AngularJS 和 Django:/uploaded_file uploadFile() 处的 TypeError 缺少 1 个必需的位置参数:“请求”