Opened 6 years ago

Closed 6 years ago

#30435 closed Bug (worksforme)

__iexact not working inside validator function.

Reported by: Diego Arturo Hernandez Fuentes Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have my query into validation to find if exist a duplicate name in my model

    def validate_name(self, value):
        if not value:
            raise serializers.ValidationError('Proporciona un valor, por favor.')
        
        try:
            exist_name = FundingSource.objects.filter(name__iexact=value)                       # here is the error __icontains is NOT WORKING
            if len(exist_name) > 0:
                raise serializers.ValidationError('Este nombre ya fue usado, por favor proporciona otro valor.')  # I need display this message in my console to be sure that exists a duplicate
        except Exception as e:
            pass
        else:
            return value

I have in my model

Funding 1
Funding 2
Funding 3
Funding 4

Next, I tried to type funding 1 to disallow me to create that name, but, the queryset display me

<QuerySet []>

Then, create new one item in my table.

Funding 1
Funding 2
Funding 3
Funding 4
funding 1

So this is wrong, your iexact is bad,

so I need that Django developer team resolve this, now.

It's urgent, please. What is the best way to develop iexact or icontains.

Thanks

My django version is 2.1.5
Django rest framework version = 3.9.0

Change History (5)

comment:1 by Diego Arturo Hernandez Fuentes, 6 years ago

Summary: __icontains NOT WORKING. URGENT!!!!!!!!!!!!!!!!!!!!!!!__iexactNOT WORKING. URGENT!!!!!!!!!!!!!!!!!!!!!!!

comment:2 by Diego Arturo Hernandez Fuentes, 6 years ago

Summary: __iexactNOT WORKING. URGENT!!!!!!!!!!!!!!!!!!!!!!!__iexact NOT WORKING. URGENT!!!!!!!!!!!!!!!!!!!!!!!

comment:3 by Ryan Govostes, 6 years ago

You are raising your ValidationError inside a try ... catch block which catches the exception and then does nothing with it. This will prevent the ValidationError from escaping your validator.

Does this work?

def validate_name(self, value):
        if not value:
            raise serializers.ValidationError('Proporciona un valor, por favor.')
        
        exist_name = FundingSource.objects.filter(name__iexact=value)
        if len(exist_name) > 0:
            raise serializers.ValidationError('Este nombre ya fue usado, por favor proporciona otro valor.')

Please keep bug report titles simple.

comment:4 by Ryan Govostes, 6 years ago

Summary: __iexact NOT WORKING. URGENT!!!!!!!!!!!!!!!!!!!!!!!__iexact not working inside validator function

comment:5 by Mariusz Felisiak, 6 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: worksforme
Status: newclosed
Summary: __iexact not working inside validator function__iexact not working inside validator function.
Version: 2.1master

First of all please don't shout. Secondly __iexact works properly, an issue is in your own code. Tip from Ryan Govostes looks reasonable, but your code will not work properly on update. You should take into account instance etc. Nevertheless it is support issue so please use one of support channels.

Note: See TracTickets for help on using tickets.
Back to Top