DJANGO createsuperuser无法正常工作... TypeError:create_superuser()缺少1个必需的位置参数:'username'

时间:2019-10-17 13:18:18

标签: django django-models django-rest-framework

我正在尝试使用Django和DRF创建RESTful API。我有一个扩展了AbstractUser的用户模型。我既无法创建普通用户,也无法创建超级用户。由于某种原因,它说

当我跑步时:

  

python manage.py createsuperuser

我收到以下错误:

“ TypeError:create_superuser()缺少1个必需的位置参数:'username'”

这是models.py文件:

std::tuple_size

serializers.py文件:

import uuid
from django.db import models
from django.conf import settings
from django.dispatch import receiver
from django.contrib.auth.models import AbstractUser
from django.utils.encoding import python_2_unicode_compatible
from django.db.models.signals import post_save
from rest_framework.authtoken.models import Token
from api.fileupload.models import File


@python_2_unicode_compatible
class User(AbstractUser):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    profile_picture = models.ForeignKey(File, on_delete=models.DO_NOTHING, null=True, blank=True)
    email = models.EmailField('Email address', unique=True)
    name = models.CharField('Name', default='', max_length=255)
    phone_no = models.CharField('Phone Number', max_length=255, unique=True)
    company_name = models.CharField('Company Name', default='', max_length=255)
    address = models.CharField('Address', default='', max_length=255)
    address_coordinates = models.CharField('Address Coordinates', default='', max_length=255)
    country = models.CharField('Country', default='', max_length=255)
    pincode = models.CharField('Pincode', default='', max_length=255)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.email


@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create_user(user=instance)

views.py文件:

from django.contrib.auth.password_validation import validate_password
from rest_framework import serializers

from .models import User
from api.fileupload.serializers import FileSerializer


class UserSerializer(serializers.ModelSerializer):
    profile_picture = FileSerializer()

    def create(self, validated_data):
        user = User.objects.create(**validated_data)
        return user

    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.company_name = validated_data.get('company_name', instance.company_name)
        instance.address = validated_data.get('address', instance.address)
        instance.country = validated_data.get('country', instance.country)
        instance.pincode = validated_data.get('pincode', instance.pincode)
        instance.phone_no = validated_data.get('phone_no', instance.phone_no)
        instance.email = validated_data.get('email', instance.email)
        instance.profile_picture = validated_data.get('profile_picture', instance.profile_picture)
        instance.save()
        return instance

    class Meta:
        unique_together = ('email',)
        model = User
        fields = (
            'id', 'password', 'email', 'name', 'phone_no', 'company_name', 'address', 'country', 'pincode', 'profile_picture',
        )
        extra_kwargs = {'password': {'write_only': True}}

1 个答案:

答案 0 :(得分:1)

使用createsuperuser命令时,将要求您输入一些必填字段,USERNAME_FIELD是必填字段,并且REQUIRED_FIELDS内的所有字段。您的代码设置为REQUIRED_FIELDS=[],因此没有必填字段,只需要添加emailpassword

但是当调用createsuperuser时,它将调用create_superuser字段的username方法:

def create_superuser(self, username, email=None, password=None, **extra_fields):

以使您输入的数据不足以将数据传递到create_superuser方法中。

要解决此问题,您只需将username添加到REQUIRED_FIELDS中即可:

REQUIRED_FIELDS = ['username']

然后,createsuperuser将要求您为username方法添加create_superuser字段。

希望有帮助。