QuerySet, Object has no attribute id - Django

倾然丶 夕夏残阳落幕 提交于 2019-11-30 03:39:19

this line of code

at = AttachedInfo.objects.filter(attachedMarker=m.id, title=title)

returns a queryset

and you are trying to access a field of it (that does not exist).

what you probably need is

at = AttachedInfo.objects.get(attachedMarker=m.id, title=title)

The reason why you are getting the error is because at is a QuerySet ie: a list. You can do something like at[0].id or use get instead of filter to get the at object.

Hope it helps!

In most cases you do not want to handle not existing objects like that. Instead of

ad[0].id

use

get_object_or_404(AttachedInfo, attachedMarker=m.id, title=title)

It is the recommended Django shortcut for that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!