4 ms·
This fails to raise an error if there is more than one object matching the given filters
by _bohm 5y ago
This fails to raise an error if there is more than one object matching the given filters
- notdonspaulding 5y agoPresumably entry_number is unique_together with blog_id. Otherwise the original code is also not handling the MultipleObjectsReturned exception. Generally speaking, I tend toward the cleanest code being: blog = Blog.objects.get(id=1) entry = blog.entry_set.filter(entry_number=1).first() if entry is None: handle_missing_entry() handle_entry(entry) But it does suffer from having the extra DB query in there, which may or may not be helpful, depending on the surrounding code (and whether or not you'll be using the blog instance anywhere else).