UserModel
class User(models.Model):
class UserGenders(models.TextChoices):
FEMALE = constants.FEMALE
MALE = constants.MALE
NOT_INFORMED = constants.NOT_INFORMED
first_name = models.CharField()
last_name = models.CharField()
email = models.EmailField(
verbose_name=constants.EMAIL_ADDRESS,
blank=True,
null=True,
unique=True
)
phone_number = PhoneNumberField(null=True, blank=True
)
gender = models.CharField(max_length=50, choices=UserGenders.choices)
birth_date = models.DateField(blank=True, null=True)
corporate_name = models.CharField(max_length=100, blank=True)
trading_name = models.CharField(max_length=100, blank=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
nickname = models.CharField(max_length=255, blank=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
class Meta:
verbose_name_plural = "Users"
def __str__(self):
return str(self.email)
ViewSet
class UserViewSet(viewsets.ModelViewSet):
queryset = acc_models.User.objects.all()
http_method_names = ['get', 'post', 'put', 'patch', 'head', 'options', 'delete']
permission_classes = (isAdmin, )
def get_serializer_class(self):
return acc_serializers.UserGETSerializer
# return acc_serializers.UserReadOnlyGETSerializer
# return acc_serializers.UserToRepresentationGETSerializer
ModelSerializer (original ~19 seconds)
class UserGETSerializer(serializers.ModelSerializer):
class Meta:
model = acc_models.User
exclude = []
ReadOnlySerializer (test #1 ~17 seconds)
class UserReadOnlyGETSerializer(serializers.ModelSerializer):
class Meta:
model = acc_models.User
fields = (
'first_name', 'last_name', 'email', 'phone_number', 'gender', 'birth_date',
'corporate_name', 'trading_name', 'is_staff', 'is_active', 'nickname',
)
read_only_fields = fields
ToRepresentationSerializer (test #2 ~ 8 seconds)
class UserToRepresentationGETSerializer(serializers.BaseSerializer):
def to_representation(self, instance):
result = {
"id": instance.id,
"first_name": instance.first_name,
"last_name": instance.last_name,
"email": instance.email,
"phone_number": str(instance.phone_number1),
"gender": instance.gender,
"birth_date": instance.birth_date,
"corporate_name": instance.corporate_name,
"trading_name": instance.trading_name,
'is_staff': instance.is_staff,
'is_active': instance.is_active,
"nickname": instance.nickname,
}
return result