Reputation: 1226
I am trying to create a RESTful API using Django and DRF. I have a User model which extends AbstractUser. I'm neither able to create normal users nor a superuser. For some reason, It says
When I run:
python manage.py createsuperuser
I get the following error:
"TypeError: create_superuser() missing 1 required positional argument: 'username'"
Here's the models.py file:
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)
The serializers.py file:
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}}
The views.py file:
from rest_framework import viewsets, mixins
from .models import User
from .serializers import UserSerializer
class UserViewSet(mixins.RetrieveModelMixin,
mixins.ListModelMixin,
mixins.CreateModelMixin,
viewsets.GenericViewSet):
queryset = User.objects.all()
permission_classes = (AllowAny,)
serializer_class = UserSerializer
Upvotes: 0
Views: 2033
Reputation: 3378
When you use createsuperuser
command, it will require you to input some required field, USERNAME_FIELD
is a required field, and all fields inside REQUIRED_FIELDS
. Your code set REQUIRED_FIELDS=[]
so there is no required field, just need to add email
and password
.
But when createsuperuser
has been called, it will call create_superuser
method which require username
field:
def create_superuser(self, username, email=None, password=None, **extra_fields):
so that your input data is not enough to pass the data into create_superuser
method.
To solve this, you can simply just add username
into REQUIRED_FIELDS
:
REQUIRED_FIELDS = ['username']
then createsuperuser
will require you to add username
field for create_superuser
method.
Hope that helps.
Upvotes: 2