Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django model retrieves same results

I have a django model, TestModel, over an SQL database.

Whenever I do

TestModel.objects.all()

I seem to be getting the same results if I run it multiple times from the same process. I tested that by manually deleting (without using ANY of the django primitives) a line from the table the model is constructed on, the query still returns the same results, even though obviously there should be less objects after the delete.

Is there a caching mechanism of some sort and django is not going to the database every time I want to retrieve the objects?

If there is, is there a way I could still force django to go to the database on each query, preferably without writing raw SQL queries?

I should also specify that by restarting the process the model once again returns the correct objects, I don't see the deleted ones anymore, but if I delete some more the issue occurs again.

like image 944
Claudiu Coman Avatar asked Dec 07 '12 16:12

Claudiu Coman


People also ask

What is __ str __ In Django model?

The __str__ method just tells Django what to print when it needs to print out an instance of the any model.

What does .values do in Django?

values() Returns a QuerySet that returns dictionaries, rather than model instances, when used as an iterable. Each of those dictionaries represents an object, with the keys corresponding to the attribute names of model objects.

How do you get or view all the items in a model in Django?

The simplest way you can get the list of objects of an attribute is to first get a query-set of that attribute alone using values_list then converting the django query-set to a python set using set() and finally to a list using list() .

Can Django model have two primary keys?

Do Django models support multiple-column primary keys? ¶ No. Only single-column primary keys are supported.


1 Answers

This is because your database isolation level is repeatable read. In a django shell all requests are enclosed in a single transaction.

Edited

You can try in your shell:

from django.db import transaction
with transaction.autocommit():
    t = TestModel.objects.all()
    ...
like image 67
dani herrera Avatar answered Sep 17 '22 18:09

dani herrera