How to create Django models that do not map to a database table

I want to create a model that does not map to a database table. Instead, it stays in memory as a python object.

In fact, this model should represent normalized data from many other displayed tables.

Other models store data that can be edited several times in one day. Due to these few changes, I do not want the table-mapped model to perform normalization / calculations and store them in the database, since these stored data can immediately become outdated.

Every time this normalized model is available (via admin), I want it to normalize data from other models from scratch (so that it displays the latest data) and behave like a normal model will be under the administrator, as shown in the list view and detailed overview for each line.

Edit after Shintoist answer:

@Shintoist Thank you for clarifying and providing a useful approach. I just implemented it, but hit a small wall at the end :)

@skirmantas: Yes, the calculations are in a separate object. This object is passed to user views.

Problem. One of the problems is that under admin.py I created a modeladminclass for this object (which does not inherit models.Model), so my custom views can override the change list view and changeview. Then I use admin.site.register () to register this model class and modeladmin. But, since this model is not a django model at all (since it is an independent python object in memory), admin.site.register () throws an error like "type is noterable". I do not want to use url.py instead of admin.py, as it is for the interface, while Im trying to override backend-admin.

+6
database django mapping models
source share
4 answers

Mmm. Thanks for all your help. The solution I came up with (with your help) looks like this:

I have two custom templates:

my_model_list.html my_model_detail.html 

In the views.py section:

 class MyModel(object): # ... Access other models # ... process / normalise data # ... store data @staff_member_required def my_model_list_view(request) #show list of all objects #. . . create objects of MyModel . . . #. . . call their processing methods . . . #. . . store in context variable . . . r = render_to_response('admin/myapp/my_model_list.html', context, RequestContext(request)) return HttpResponse(r) @staff_member_required def my_model_detail_view(request, row_id) # Shows one row (all values in the object) in detail #. . . create object of MyModel . . . #. . . call it methods . . . #. . . store in context variable . . . r = render_to_response('admin/myapp/my_model_detail.html', context, RequestContext(request)) return HttpResponse(r) 

In the main django urls.py:

 urlpatterns = patterns( '', (r'^admin/myapp/mymodel/$', my_model_list_view), (r'^admin/myapp/mymodel/(\d+)/$', my_model_detail_view), ( r'^admin/', include( admin.site.urls ) ) ) 

As you already noticed, I had to embed url patterns in the url.py file. I do not know if this is the best way to do this, as I believe the url.py file is not intended for admin-related pages. This is only for the site interface.

0
source share

How to use several databases and configure one of them to use tables in memory?

For MySQL, it will 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 when creating tables you need to use SET storage_engine , but it may be that it does not add too much overhead in any case for your use.

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

+6
source share

Why is the model at all? Match your calculations in the view, write a template for it and require authorization to enter the system. This recreates this normalized data only when the page loads and will ever exist in memory, saving you resources.

+2
source share

Depending on how complex these “calculations” are, it looks like you need a custom kind of database (supported, I believe, SQLite, MySQL, Postgres and Oracle, at least) used in conjunction with the model with Meta.managed=False .

If you are lucky, you can get the South to create it for you during the migration process, but so far it does not seem that the south supports the views.

+1
source share

All Articles