Thank you Aaron, I started to figure this all out too as I read the docs 
more carefully and looked at the source code more thoughtfully.

One nuance on the try/except block you propose: doesn't work as expected in 
the request framework because even though the except block can be stated as 
you propose, the exception gets thrown by db.models query.py and is 
declared as a simple
raise DoesNotExist...

And this then bubbles all the way back through the middleware.

The simplest answer for my use case is to just use filter().

As get() is defined as a method of the QuerySet class, at first glance I 
assumed it would return a queryset, but as you point out, it doesn't. It 
returns an instance of a model class.  In my perfect world, get() would 
have been named as get_as_model_object so that it was clear this is a 
convenience method of the class. But that's just quibbling. Had I read the 
source code more carefully, it would have dawned on me what to do and why I 
had been confused.

Thank you.
steve

On Monday, October 23, 2017 at 2:49:28 PM UTC-7, st...@fitcode.com wrote:
>
> I have been tripping over the following exception
> DoesNotExist: INSERT_YOUR_MODEL_NAME_HERE matching query does not exist.
>
> The error is source in django 1.11.2 in db.models query.py file, in the 
> get(self, *args, **kwargs) function, line 378 to be exact
> ...
>     raise self.model.DoesNotExist(...)
> ...
> This throws a 500 error and I would like my code not to throw a 500 error 
> in this situation, rather a customized message more along a 4xx error e.g.
> {"message": "record matching identifier A and identifier B not found in 
> database"}
> rather than a 500 error.
>
> It seems to me that my only choice right now is to subclass QuerySet and 
> write my own version of the get(self, *args, **kwargs) method. Is that my 
> only choice or is there a django parameter I can set so that 0 matching 
> results does not return a 500?
>
> Or do I need to intercept the 500 error somewhere else.
>
> Sorry, am a bit of a newbie to django internals, all help and advice is 
> appreciated.
>
> Thanks,
> Steve
>

-- 
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+unsubscr...@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/775408b6-0ece-486a-9026-1c09ba322224%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to