Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django models without database

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!

like image 864
dlitwak Avatar asked Feb 01 '12 05:02

dlitwak


People also ask

Can Django work without database?

However, while you can use django with no database, the object-relational mapper is pretty much its first and foremost advertised feature. Django was designed to produce database-backed web sites, so if you're not going to use a database you might end up dealing with a bunch of unnecessary hassle.

Is Django models a database?

The basics: Each model is a Python class that subclasses django.db.models.Model . Each attribute of the model represents a database field. With all of this, Django gives you an automatically-generated database-access API; see Making queries.

Can you use Django without models?

It is completely possible to create a Django project without any models. You only really need models if your website contains objects, like posts or users.

Does Django automatically create database?

Django doesn't create databases for you automatically. You have to do this yourself manually. This is a simple package that creates your database for you automatically, if necessary, when you run migrate for the first time. Important: This package only supports PostgreSQL at the moment!


2 Answers

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

like image 98
andorov Avatar answered Oct 01 '22 17:10

andorov


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.

like image 35
John Mee Avatar answered Oct 01 '22 17:10

John Mee