History: Error on user = User.objects.get(pk=u.pk) , - DoesNotExist . In general, I can not understand why. Handled this line with an exception, now it does not go into try at all. Where did I go wrong and why does this error go out?
PS The database is fine. In another code, everything works. I also checked the data. The essence of this algorithm is that it searches for applications in two cities, i.e. proud of sending and the city of arrival. After that, he searches in the application! all signed people to it. Then I add an object to the userList ["user"]. Then I do a check: If the number of seats (indicated in Ad) is greater than the users who subscribe to this application, then this application has free places and I add this application to the adList ["name"] list. And return the user. Everything.
def searchAd(request, ad): j = json.loads(ad) a = Ad(**j) adList = {"name": []} userList = {"user":[]} if not Ad.objects.filter(cityStart=a.cityStart, cityEnd=a.cityEnd): return HttpResponse("null") for ad in Ad.objects.filter(cityStart=a.cityStart, cityEnd=a.cityEnd): for u in User.adPk.through.objects.filter(ad_id=ad.pk): try: user = User.objects.get(pk=u.pk) userList["user"].append(json.dumps({'name': user.name, 'serName': user.serName, 'email': user.email, 'password': user.password,'pk': user.pk}, ensure_ascii=False)) except Exception: pass if ad.countSits >= userList["user"].__len__(): adList["name"].append(json.dumps({'name': ad.name, 'timeStart': ad.timeStart, 'timeEnd': ad.timeEnd, 'cityStart': ad.cityStart,'cityEnd': ad.cityEnd, 'about': ad.about, 'driver': ad.driver, 'price': ad.price,'countSits': ad.countSits, 'pk': ad.pk}, ensure_ascii=False)) return HttpResponse(json.dumps(adList, ensure_ascii=False))
User.DoesNotExistinstead ofException. - MichaelPak