I modified the Auth User by using AbstractUser Class. Registered it in settings. Everything else is working I can create an instance using the User Model BUT
The Problem comes during the serialization. My serializer doesn’t throw any error, but any time I try and use the serializer.data I get AttributeError: ‘str’ object has no attribute ‘_meta’.
User Model
class User(AbstractUser): # Add additional fields here email = models.EmailField(max_length=254, unique=True) name = models.CharField(max_length=100) username = models.CharField(max_length=100) password = models.CharField(max_length=100) is_staff = models.BooleanField(default=False) is_active = models.BooleanField(default=False) date_joined = models.DateTimeField(auto_now_add=True) last_login = models.DateTimeField(auto_now=True) first_name=None last_name=None USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['name', 'username', 'password'] objects = CustomUserManager() def __str__(self): return self.email # Ensure that the password is hashed before saving it to the database def save(self, *args, **kwargs): self.password = make_password(self.password) super(User, self).save(*args, **kwargs)
Custom Manager
from django.contrib.auth.base_user import BaseUserManager from django.utils.translation import gettext_lazy as _ class CustomUserManager(BaseUserManager): def create_user(self, email, password, **extra_fields): """ Create and save a User with the given email and password. """ if not email: raise ValueError(_('The Email must be set')) email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save() return user def create_superuser(self, email, password, **extra_fields): """ Create and save a SuperUser with the given email and password. """ extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) extra_fields.setdefault('is_active', True) if extra_fields.get('is_staff') is not True: raise ValueError(_('Superuser must have is_staff=True.')) if extra_fields.get('is_superuser') is not True: raise ValueError(_('Superuser must have is_superuser=True.')) return self.create_user(email, password, **extra_fields)
User Serializer
from rest_framework import serializers from django.conf import settings User = settings.AUTH_USER_MODEL class UserSerializer(serializers.ModelSerializer): print("UserSerializer") class Meta: model = User # fields = (['id', 'username', 'email', 'name']) fields = '__all__'
View
@api_view(['POST']) def createUser(request): data = request.data serializer = UserSerializer(data,many=False) if serializer.is_valid(): print(type(serializer.data)) serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
I’ve stuck with the code for the last 7-8 hours. Even tried specifying the fields but I haven’t been able to figure out why this is happening.
Advertisement
Answer
You should work with the get_user_model()
function [Django-doc] to obtain a reference to the user model, the AUTH_USER_MODEL
setting [Django-doc] is only a string to the name of that model:
from rest_framework import serializers from django.contrib.auth import get_user_model class UserSerializer(serializers.ModelSerializer): class Meta: model = get_user_model() fields = '__all__'