26👍
You’ll want to use the update
method since you’re dealing with multiple objects:
https://docs.djangoproject.com/en/2.0/topics/db/queries/#updating-multiple-objects-at-once
21👍
filter returns a queryset. A queryset isn’t a single object, it’s a group of objects so it doesn’t make sense to call save() on a queryset. Instead you save each individual object IN the queryset:
game_participants = GameParticipant.objects.filter(player=player, game=game)
for object in game_participants:
object.save()
- [Django]-Import error 'force_text' from 'django.utils.encoding'
- [Django]-Django: Example of generic relations using the contenttypes framework?
- [Django]-How to include templates dynamically in Django using "include" tag
2👍
It is possible to get this error by assigning not saved object to another object foreign field.
for project in projects:
project.day = day
day.save()
and the right way of this is:
day.save()
for project in projects:
project.day = day
- [Django]-Django and domain driven design
- [Django]-Get path of virtual environment in pipenv
- [Django]-Setting up a foreign key to an abstract base class with Django
2👍
filter return a list and if your want a specific single object from it you need give the index of that object
game_participant = GameParticipant.objects.filter(player=player, game=game)[0]
- [Django]-Django javascript files
- [Django]-Turn off automatic pagination of Django Rest Framework ModelViewSet
- [Django]-How to configure where to redirect after a log out in Django?
2👍
If you use filter() function then you must loop the data, then use save() function
std = Student.objects.filter(player=player, game=game)
for i in std:
i.name = "somthing"
i.save()
If you use the get() function then use the save() function without a loop.
std = Student.objects.get(player=player, game=game)
std.name = "somthing"
std.save()
- [Django]-Atomic operations in Django?
- [Django]-Django serializer inherit and extend fields
- [Django]-Django batching/bulk update_or_create?
0👍
def Clearlist(request):
que = Feeds.objects.filter(selected=True)
for i in que:
i.selected = False
i.save()
return redirect('core:all')
this is a good way to save a into queryset
- [Django]-How to send a POST request using django?
- [Django]-(13: Permission denied) while connecting to upstream:[nginx]
- [Django]-Pass request context to serializer from Viewset in Django Rest Framework
0👍
New in Django 2.2: bulk_update
objs = [
Entry.objects.create(headline='Entry 1'),
Entry.objects.create(headline='Entry 2'),
]
objs[0].headline = 'This is entry 1'
objs[1].headline = 'This is entry 2'
Entry.objects.bulk_update(objs, ['headline'])
- [Django]-Distributing Django projects with unique SECRET_KEYs
- [Django]-How to reload modules in django shell?
- [Django]-How to write a query to get find value in a json field in django
0👍
when you use the queryset maybe it return a list that why u should use
game_participant = GameParticipant.objects.filter(player=player, game=game)[0]
instead of
game_participant = GameParticipant.objects.filter(player=player, game=game)
try this it work for me
- [Django]-How to solve "Page not found (404)" error in Django?
- [Django]-You are trying to add a non-nullable field 'new_field' to userprofile without a default
- [Django]-Django migrate : doesn't create tables