django models without database
Solution 1
Another option may be to use:
class Meta:
managed = False
to prevent Django from creating a database table.
https://docs.djangoproject.com/en/2.2/ref/models/options/#managed
Solution 2
Just sounds like a regular Class
to me.
You can put it into models.py
if you like, just don't subclass it on django.db.models.Model
. Or you can put it in any python file imported into the scope of whereever you want to use it.
Perhaps use the middleware to instantiate it when request comes in and discard when request is finished. One access strategy might be to attach it to the request object itself but ymmv.
Solution 3
Unlike SQLAlchemy, django's ORM does not support querying on the model without a database backend.
Your choices are limited to using a SQLite in-memory database, or to use third party applications like dqms which provide a pure in-memory backend for django's ORM.
Solution 4
Use Django's cache framework to store data and share it between views.
dlitwak
www.davidlitwak.com, www.litwakrecommends.com. Interested in Product Management, Software Engineering, Web Development, Entrepreneurship and International Business. At some point in my life I would like to work abroad, possibly in an emerging market or in Europe, but as of now I am based in Silicon Valley. I have my EU citizenship (Estonia) so I am qualified to work in any EU country without visa hassles.
Updated on January 29, 2020Comments
-
dlitwak almost 4 years
I know the automatic setting is to have any models you define in models.py become database tables.
I am trying to define models that won't be tables. They need to store dynamic data (that we get and configure from APIs), every time a user searches for something. This data needs to be assembled, and then when the user is finished, discarded.
previously I was using database tables for this. It allowed me to do things like "Trips.objects.all" in any view, and pass that to any template, since it all came from one data source. I've heard you can just not "save" the model instantiation, and then it doesn't save to the database, but I need to access this data (that I've assembled in one view), in multiple other views, to manipulate it and display it . . . if i don't save i can't access it, if i do save, then its in a database (which would have concurrency issues with multiple users)
I don't really want to pass around a dictionary/list, and I'm not even sure how i was do that if I had to.
ideas?
Thanks!
-
dlitwak almost 12 yearsAlready tried, if you store instances of a model in a session, you can retrieve it, but you can't use all the normal methods on the model . . . for instance "order_by" . . . i suppose that is because those translate to sql and we don't have a database anymore, but still . . perhaps i'm searching for something that doesn't exist
-
schoettl almost 8 yearsWhere do I best put this regular class?
models.py
? -
user1158559 almost 7 yearsUsing models instead of regular classes can sometimes be advantageous, i.e. when using something like django-rest-swagger to generate API docs.
-
user1158559 almost 7 years
models.py
is an appropriate place for such a model -
John Mee almost 7 years@jakob In python you can put the classes in any
.py
file. To use a classimport
it. That's what all thosefrom mymodule import MyClass
statements are doing. Checkout packages -
Jason Fuerstenberg about 6 yearsThe poster mentioned he wants to use a Django model without it being backed by a table. The Django documentation you linked here specifically mentions that only the migration and deletion operations will not be performed, while all other table-related operations will continue to work as if the table exists.
-
zepp133 over 5 yearsThis is not accurate. Models with
managed = False
will still create tables. -
Manu Artero about 4 yearsWe're using this option, gathering regular classes in a module called
domain_models.py
-
Kal almost 3 years@zepp.lee Maybe you were referring to an older version, but the current docs explicitly say "If [managed =] False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means." (docs.djangoproject.com/en/3.1/ref/models/options/#managed). So the model's functionality assumes that there is a database table mapped to the model for querying, etc., but it doesn't do anything to ensure the database table's existence (i.e. doesn't manage it for you)
-
blockhead about 2 yearsThis is different from what is being asked.