Monday, October 29, 2018

Re: Identify failed insert field

Yes, usually I print request.POST to check.
On Mon, 29 Oct 2018 at 19:11, Manjunath <dmanju.manju58@gmail.com> wrote:
>
> I think one of the numeric values you are passing is an empty string.
> Django is trying to cast it to int but failing to do so.
>
> Best solution would be to print each values before save() call & you will know which is the error causing column..
>
> Hope it helps!!
>
>
> On Sunday, October 28, 2018 at 8:35:13 AM UTC+5:30, Joel wrote:
>>
>> Is there anyway to identify which database field update failed,
>> instead of a generic message like "ValueError: invalid literal for
>> int() with base 10: ''?
>>
>> For example, I have the following save():
>>
>> tempcust = unconfirmedappointment(name=name, ageyrs=ageyrs,
>> agemnths=agemnths, gender=gender, mobile=phone,
>> docid=doc,clinicid=clinicobj, seldate=seldate, seltime=slot,
>> email=email, address=address, city=city, uniquestring=uniquestring,
>> otp=otp)
>> tempcust.save()
>>
>> Apparently one of the values is wrong for the field. But all I get is
>> the following:
>>
>> 2018-10-28 08:29:48,842 django.request ERROR Internal Server Error:
>> /clinic/madhav/doctor/4/setappointment
>> Traceback (most recent call last):
>> File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/exception.py",
>> line 34, in inner
>> response = get_response(request)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py",
>> line 126, in _get_response
>> response = self.process_exception_by_middleware(e, request)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py",
>> line 124, in _get_response
>> response = wrapped_callback(request, *callback_args, **callback_kwargs)
>> File "/home/joel/myappointments/clinic/views.py", line 896, in setappointment
>> tempcust.save()
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/base.py",
>> line 718, in save
>> force_update=force_update, update_fields=update_fields)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/base.py",
>> line 748, in save_base
>> updated = self._save_table(raw, cls, force_insert, force_update,
>> using, update_fields)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/base.py",
>> line 831, in _save_table
>> result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/base.py",
>> line 869, in _do_insert
>> using=using, raw=raw)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/manager.py",
>> line 82, in manager_method
>> return getattr(self.get_queryset(), name)(*args, **kwargs)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/query.py",
>> line 1136, in _insert
>> return query.get_compiler(using=using).execute_sql(return_id)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
>> line 1288, in execute_sql
>> for sql, params in self.as_sql():
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
>> line 1241, in as_sql
>> for obj in self.query.objs
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
>> line 1241, in <listcomp>
>> for obj in self.query.objs
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
>> line 1240, in <listcomp>
>> [self.prepare_value(field, self.pre_save_val(field, obj)) for
>> field in fields]
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
>> line 1182, in prepare_value
>> value = field.get_db_prep_save(value, connection=self.connection)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
>> line 790, in get_db_prep_save
>> return self.get_db_prep_value(value, connection=connection, prepared=False)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
>> line 785, in get_db_prep_value
>> value = self.get_prep_value(value)
>> File "/home/joel/.local/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
>> line 1807, in get_prep_value
>> return int(value)
>> ValueError: invalid literal for int() with base 10: ''
>>
>> How to trace which field is problematic, other than manually debugging
>> each variable and seeing if it is non integer? Can django tell me
>> which field is causing the issue?
>>
>> Sincerely yours,
>>
>> Dr Joel G Mathew
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bdba25e4-0d9f-4031-a9ca-9ce6016c085c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw_8QOceZK3Cd7pAJ17TZuZaSt3fp8NiHZht3b2CtjQwKvg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment