ー PupSoZeyDe ー
ー PupSoZeyDe ー

Reputation: 1392

Django: object has no attribute 'update'

I want to update first one record of the database.

This result can be two or more results. This works.

TelegramAppUserConfig.objects.filter(user=user).update(json="hoge")

This should result just one result. But this doesn't work. Django: object has no attribute 'update'

TelegramAppUserConfig.objects.filter(user=user).first().update(json="hoge")
TelegramAppUserConfig.objects.filter(user=user)[0].update(json="hoge")

How can I code for that? Thanks.

Upvotes: 1

Views: 2013

Answers (2)

ajay.16nk
ajay.16nk

Reputation: 92

.update method is callable on queryset not on a single object e.g. TelegramAppUserConfig in your case. To get a queryset containing only the first item you can use TelegramAppUserConfig.objects.filter(user=user) [0:1]. So you can update it as

TelegramAppUserConfig.objects.filter(user=user) [0:1].update(json="hoge")

Upvotes: 1

Syntactic Fructose
Syntactic Fructose

Reputation: 20104

When dealing with single objects, you can always just use save() to persist any changes:

config = TelegramAppUserConfig.objects.filter(user=user).first()

config.json = "hoge"
config.save()

Upvotes: 1

Related Questions