Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a Django models that doesn't map to a database table

I want to create a model that doesn't map to a database table. Instead, stays in memory as a python object.

Actually, this model is supposed to represents normalised data from many other table-mapped models.

The other models store data which can be edited multiple times in a single day. Because of these multiple edits, I don't want a table-mapped-model that performs normalisations/calculations and stores them in a database as, this stored data can go out of date right away.

Every time this normalised model is accessed (via admin), I want it to perform the normalisations on data from the other models from scratch (So that it can show the most up to date data) and behave just like a normal model would under admin like Showing the list view and a detailed view for each row.

Edit after Shintoist's answer:

@Shintoist Thanks for the clearing things out and providing a usable approach. I have just implemented it but hitting a small wall in the end :)

@skirmantas: Yes, the calculations are in a separate object. This object is being passed into the custom views.

Problem: One problem is that under admin.py, I have created an modeladminclass for this object(which doesn't inherit models.Model) so my custom views can overide the changelist view and changeview. I then use admin.site.register() to register this model-like class and the modeladmin. But, since this model is not a django model at all (as it is an independant python object in memory) admin.site.register() throws a " 'type' object is not iterable" error. I don't want to use the url.py instead of admin.py as it's meant for the frontend while Im trying to overide the backend-admin.

like image 858
sysasa Avatar asked Nov 10 '10 13:11

sysasa


People also ask

Can Django be used without a database?

Although you can use Django without a database, it comes with an object-relational mapper in which you describe your database layout in Python code.

What is Foreignkey Django models?

Introduction to Django Foreign Key. A foreign key is a process through which the fields of one table can be used in another table flexibly. So, two different tables can be easily linked by means of the foreign key. This linking of the two tables can be easily achieved by means of foreign key processes.

How does Django interact with database?

By default, Django works with SQLite, database and allows configuring for other databases as well. Database connectivity requires all the connection details such as database name, user credentials, hostname drive name etc. To connect with MySQL, django. db.


2 Answers

What about using multiple databases, and configuring one of them to use in-memory tables?

For MySQL it would look like this:

DATABASES = {
    'default': {
    },
    'memory': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbname',
        'USER': 'dbuser',
        'PASSWORD': '',  
        'HOST': 'localhost',
        'PORT': '',         

        'OPTIONS': {"init_command": "SET storage_engine=MEMORY"}
    }
}

Note that you only need to use SET storage_engine when creating tables, but it might be that it doesn't add too much overhead anyway for your use case.

http://dev.mysql.com/doc/refman/5.0/en/memory-storage-engine.html

like image 171
Tomasz Zieliński Avatar answered Nov 15 '22 08:11

Tomasz Zieliński


Why have a model at all? Reference your calculations in a view, write a template for it and require admin login to access it. That would recreate this normalised data only when you load the page and would only ever exist in memory, saving you resources.

like image 43
Johanna Larsson Avatar answered Nov 15 '22 09:11

Johanna Larsson