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 ?

Ahmad Mardene
  • 47
  • 1
  • 6

2 Answers2

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
        )
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • it is MinValueValidator,MaxLengthValidator not MinValidator, MaxValidator , i tried it and the same error "object of type 'int' has no len()" – Ahmad Mardene Apr 29 '20 at 19:44
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!

wgarlock
  • 96
  • 6
  • 1
    i made it charfield and it worked thanks! , any ideas how i can make it start with specific number like "01" ? – Ahmad Mardene Apr 29 '20 at 19:49
  • 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 https://stackoverflow.com/questions/4269605/django-override-save-for-model – wgarlock Apr 29 '20 at 19:57
  • no i dont mean that ,, i want the model only accept client_identity_id which start with "01" – Ahmad Mardene Apr 29 '20 at 22:14
  • I would probably write a custom validator in the save method. https://docs.djangoproject.com/en/3.0/ref/validators/. Since strings are iterable, you could validate on client_identity_key[0:1] == “01” – wgarlock Apr 30 '20 at 05:33