Reputation: 13
Let's say I store my query result temporarily to a variable
temp_doc = Document.objects.filter(detail=res)
and then I want to insert some data in said model and will be something like this
p = Document(detail=res)
p.save()
note that res
are object from other model to make some FK relation.
For some reason the temp_doc
will contain the new data.
Are .filter() supposed to work like that?
Because with .get() the data inside temp_doc
don't change
Upvotes: 1
Views: 64
Reputation: 15728
Django Querysets are lazy, this behavior is well documented
QuerySets are lazy – the act of creating a QuerySet doesn’t involve any database activity. You can stack filters together all day long, and Django won’t actually run the query until the QuerySet is evaluated.
So basically that means until you don't ask for data evaluation, database query won't be executed
In your example
temp_doc = Document.objects.filter(detail=res)
p = Document(detail=res)
p.save()
enter code here
evaluating temp_doc now would include newly created Document as database query would return it
simply constructing list would evaluate QuerySet at the start
#evaluation happens here
list(temp_doc) = Document.objects.filter(detail=res)
p = Document(detail=res)
p.save()
Upvotes: 1