首先,我知道有关此TypeError的一些答案,但没有一个能解决我的情况。我做了研究,所以才发布这个问题。
我在sutck出错时说Django / djangorestframework中的TypeError:无法散列的类型:'list'。
我什至不知道错误的位置,因为对我来说,回溯并不是很清楚。
这是我的代码:
serializers.py:
from rest_framework import serializers
from .models import User
class UserSerializer(serializers.ModelSerializer):
"""User model serializer"""
class Meta:
model = User
fields = ('id', 'email', 'username', 'password')
extra_kwargs = {
'password': {
'write_only': True,
'style': {'input': 'password'}
}
}
def create(self, validated_data):
"""Create and return a new user"""
user = User.objects.create_user(email=validated_data['email'], username=validated_data['username'], password=validated_data['password'])
return user
跟踪:
Traceback (most recent call last):
File "C:\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Python36\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Python36\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Python36\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "C:\Python36\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Python36\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python36\lib\site-packages\django\contrib\auth\checks.py", line 50, in check_user_model if not cls._meta.get_field(cls.USERNAME_FIELD).unique:
File "C:\Python36\lib\site-packages\django\db\models\options.py", line 551, in get_field
return self._forward_fields_map[field_name]
TypeError: unhashable type: 'list'
models.py:
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin
from django.utils import timezone
class UserManager(BaseUserManager):
"""Manager for User model"""
def create_user(self, email, username, password=None):
"""Function for creating a user"""
if not email:
return ValueError("Email must be provided")
email = self.normalize_email(email)
user = self.model(email=email, username=username)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
"""Function for creating superusers"""
user = self.create_user(email, username, password)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
"""User database model"""
email = models.EmailField(max_length=255)
username = models.CharField(max_length=50)
created_at = models.DateTimeField(default=timezone.now)
upvotes = models.IntegerField(default=0)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = ['email']
REQUIRED_FIELDS = ['username']
def __str__(self):
return self.username
我认为问题出在我的序列化程序中,但是如果您需要我提供其他文件,请发表评论,我将更新问题。
答案 0 :(得分:3)
您收到的错误是因为底层代码试图获取User模型的用户名字段,但是您将其设置为列表而不是字符串,这意味着它找不到指定的字段。
将USERNAME_FIELD = ['email']
更改为USERNAME_FIELD = 'email'