2

my model.py is :

from django.core.validators import MinLengthValidator,MaxLengthValidator

    class clients(models.Model):
        client_identity_id = models.IntegerField(validators=[MaxLengthValidator(11),MinLengthValidator(11)], unique=True)

        '
        '

my serializers.py is :

class UserSerializer(serializers.ModelSerializer):
    #money = serializers.IntegerField(required=False)
    class Meta:
        model = clients
        fields = ('client_identity_id','client_id','client_firstname','client_middlename','client_lastname','client_country','money','client_num')
        read_only_fields = ('money','client_id')
    def create(self, validated_data, **kwargs):
        validated_data['client_id'] = ''.join(secrets.choice(string.ascii_uppercase + string.digits) 
                                              for i in range(8))

        return clients.objects.create(**validated_data)

my views.py is :

def post(self,request):
    data=request.data
    serializer = UserSerializer(data=data)
    if serializer.is_valid():

        serializer.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

but when i make a post request with client_identity_id=12345678910 it keep telling me "object of type 'int' has no len()" how can i fix that please ?

2 Answers 2

1

A digit with 11 digits ranges between 10000000000 and 99999999999, so you can work with:

from django.core.validators import MinValueValidator, MaxValueValidator

class clients(models.Model):
        client_identity_id = models.IntegerField(
            validators=[
                MinValueValidator(10_000_000_000),
                MaxValueValidator(99_999_999_999)
            ],
            unique=True
        )
Sign up to request clarification or add additional context in comments.

1 Comment

it is MinValueValidator,MaxLengthValidator not MinValidator, MaxValidator , i tried it and the same error "object of type 'int' has no len()"
0

The error you’re receiving is a python error. Integers are not iterable objects in python. I assume your validators rely on the len() method which only works on iterable objects (strings, lists, etc.) I recommend you change your field to a CharField() or create a custom validator to check the scientific notation value of your integer. Example. An 11 character like 10000000000 would be 1 x 10^11. Hint the exponent is 11 here. I hope this helps!

4 Comments

i made it charfield and it worked thanks! , any ideas how i can make it start with specific number like "01" ?
Google around for “prepend python string” there’s a ton of ways you could do this. If you wanted to add 01 to every single client_identity_id turning a 11 char entry to a 13 char entry, over write the def save() method. Before you call super(...).save() prepend “01” to self.client_identity_field. Here’s a link to overriding save stackoverflow.com/questions/4269605/…
no i dont mean that ,, i want the model only accept client_identity_id which start with "01"
I would probably write a custom validator in the save method. docs.djangoproject.com/en/3.0/ref/validators. Since strings are iterable, you could validate on client_identity_key[0:1] == “01”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.