Reputation: 1392
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
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
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