{{{
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
--
Ticket URL: <https://code.djangoproject.com/ticket/30435>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
Ticket URL: <https://code.djangoproject.com/ticket/30435#comment:1>
--
Ticket URL: <https://code.djangoproject.com/ticket/30435#comment:2>
Comment (by Ryan Govostes):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/30435#comment:3>
--
Ticket URL: <https://code.djangoproject.com/ticket/30435#comment:4>
* status: new => closed
* resolution: => worksforme
* version: 2.1 => master
* component: Uncategorized => Database layer (models, ORM)
Comment:
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
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels].
--
Ticket URL: <https://code.djangoproject.com/ticket/30435#comment:5>