Serializing BigIntegerField, TextField in a Django serializer

I have a model that has the following attributes

from django.db import models
class ApiLogs(models.Model):
    user_id = models.BigIntegerField(null=True)
    ip = models.CharField(max_length=16)
    user_agent = models.TextField(blank=True, null=True)
    client = models.CharField(max_length=50, blank=True, null=True)
    client_version = models.CharField(max_length=50, blank=True, null=True)
    token = models.TextField(blank=True, null=True)
    uri = models.CharField(max_length=200)
    method = models.CharField(max_length=20)

I defined a serializer

from rest_framework import serializers
class ApiSerializer(serializers.Serializer):
    user_id = serializers.BigIntegerField( allow_null=True)
    ip = serializers.CharField(max_length=16)
    user_agent = serializers.TextField(allow_blank=True, allow_null=True)
    client = serializers.CharField(max_length=50, allow_blank=True, allow_null=True)
    client_version = serializers.CharField(max_length=50, allow_blank=True, allow_null=True)
    token = serializers.TextField(allow_blank=True, allow_null=True)
    uri = serializers.CharField(max_length=200)
    method = serializers.CharField(max_length=20)

But it shows an error something like this:

user_id = serializers.BigIntegerField( allow_null=True)
AttributeError: 'module' object has no attribute 'BigIntegerField'

for text box

user_agent = serializers.TextField(allow_blank=True, allow_null=True)
AttributeError: 'module' object has no attribute 'TextField'

Now how to serialize this data type.

+4
source share
2 answers

This is due to the fact that the Serializer django rest Framework does not have a TextField. Where your model has a TextField, you need to use CharField in the serializer.

CharField Text view. Optionally checks that the text is shorter than max_length and longer than min_length.

django.db.models.fields.CharField django.db.models.fields.TextField.

BigIntegerFields , , IntegerField , .

+5

your_variable_name = serializers.CharField(style={'base_template': 'textarea.html'})

.

0

Source: https://habr.com/ru/post/1650658/


All Articles