Django error object has no attribute 'update'

后端 未结 4 1000
[愿得一人]
[愿得一人] 2020-12-15 04:34

UPDATE

I was doing some maintenance on the server and rebooted... once it came back the code worked just fine... which actually makes me to worry ju

4条回答
  •  时光取名叫无心
    2020-12-15 04:56

    I have had similar case, but it worked when using construction like:

    this_spot = Spot.objects.filter(pk=obj.spot.pk)
    this_spot.update(friendly_rate=rating_to_be_persisted)
    

    but not working in case, where I wanted access directly single instance, e.g from foreign key side class. Returning 'Spot' object has no attribute 'update'.

    The reason is simply the way update() works described in django documentation:

    The way around is approach like shown on the django site:

    >>> b = Blog.objects.get(pk=1)
    
    # Update all the headlines belonging to this Blog.
    >>> Entry.objects.select_related().filter(blog=b).update(headline='Everything is the same')
    

提交回复
热议问题