On Jul 27, 1:53 pm, Josh <jos.carpente...@yahoo.com> wrote: Daniel already answered, just a couple observations:
> 1. the id of the record is not known. Given your question ("update (...) fields in a model", which I assume mean : "update (...) fields in a model _instance_"), you obviously know the pk either directly (passed as argument etc), indirectly (you have some lookup that will retrieve the model instance), or of course thru the instance itself (if you already have it). > 2. Using the samples in the query-documentation. Create another query to get > a single object in a queryset using: > n = SomeModel.objects.get(id=myid) > Somemodel.objects.filter(pk=n).update(somefield=newfielddata) the "pk" lookup shortcut expected a litteral value, not a model instance, so, assuming your model's id field is the pk (which is usually the case), this should be: n = SomeModel.objects.get(id=myid) Somemodel.objects.filter(pk=n.id).update(somefield=newfielddata) which is a overcomplicated (and rather inefficient) way to write: Somemodel.objects.filter(pk=myid).update(somefield=newfielddata) > m.save() Did you mean 'n' ? If yes, a call to n.save() at this point will overwrite the previous update. > I was thinking to try a different approach. When a record exists, retrieve > all existing data and add this to the .save(). This seems like a pretty > cumbersome way to achieve updating a few fields. Indeed !-) > What is the best way to > achieve this? Depends on quite a few things. Queryset.update is atomic - which can avoids some problems with concurrent access - and is quite fast. OTHO, it won't trigger the pre_save and post_save signals, nor even call on your model's save method, so if your code relies on either a custom save method and/or pre/post save signals, you might screw it up. Most often, you'll be safer using Daniel's solution for most things and only using Queryset.update for batch updates if and when you know you don't have any python code triggered by yourmodel.save() -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.