Django multi table on same model

227 views
Skip to first unread message

Kalvin Handilist

unread,
Jul 13, 2017, 1:39:15 PM7/13/17
to Django users

I have huge table that needed to be sliced into some smaller table, ex: campaign_01, campaign_02, ... While using django queryset with different table name for same model, what I only know to set table name on a model is: Model._meta.db_table = 'tableXXX'

However this method doesn't work in single shell/request. (only work for first time, but not for the next) -> maybe because it still on same instance? After the second time we tried to set _meta.db_table = 'tableYYY', it will occur an error "django.db.utils.ProgrammingError: missing FROM-clause entry for table "tableXXX""

I also have tried some suggestion I read for this problem answer like:

class ListingManager(models.Manager):
    def get_custom_obj(self, table_name):
        self.model._meta.db_table = table_name
        return self

class ObjectName(models.Model):
    objects = ListingManager()

Try to create an Object manager to get new object, but it not work, it still throw same error as before (on the second time setting _meta.db_table)

The only way to make it work if we want to set multiple times for _meta.db_table is we need to exit() the shell first, then re-enter the shell mode (which means for loop is not gonna work).

I know it can be achieved with raw query 'Insert into tableXXX values ()', but any method to do it using django queryset? Thanks~

Christian Ledermann

unread,
Jul 14, 2017, 5:19:40 AM7/14/17
to django...@googlegroups.com
What problem are you trying to solve and why this way?
How many rows does your table have?
What database are you using?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fba2ca9e-874f-45b8-b285-3c116fefb036%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best Regards,

Christian Ledermann

Newark-on-Trent - UK
Mobile : +44 7474997517

https://uk.linkedin.com/in/christianledermann
https://github.com/cleder/


<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don’t drive species to extinction

2) Don’t destroy a habitat that species rely on.

3) Don’t change the climate in ways that will result in the above.

}<(((*>

Guilherme Leal

unread,
Jul 14, 2017, 7:51:50 AM7/14/17
to django...@googlegroups.com
That kind of table partitioning, usualy is made on the database level, not on the application level. You should check your database documentation to learn how implement that logic.

On PostgreSQL, check this.

Chew Kok Hoor (gMail)

unread,
Jul 14, 2017, 8:23:02 AM7/14/17
to django...@googlegroups.com
Hi,

    I tried overriding the map template using bold line below:
class LocationAdmin(geo_admin.OSMGeoAdmin):
form = LocationForm
map_template = 'gis/admin/google.html

In my settings.py I have added my folder:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.static',
],
'debug': DEBUG
},
}
]

and my files are properly located:


However, when my admin page loads, it seems to not load from my template but instead load from django/forms/templates/ (as per error).

Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:

  • django.template.loaders.filesystem.Loader: ***/lib/python2.7/site-packages/django/forms/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/django/contrib/auth/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/django/forms/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/filebrowser/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/django/contrib/admin/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/django/contrib/gis/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/tinymce/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/ios_notifications/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/adminsortable/templates/gis/admin/google.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: ***/lib/python2.7/site-packages/django_mptt_admin/templates/gis/admin/google.html (Source does not exist)

Any idea why it is not picking up, and is there any way to overwrite the map widget (or any other widgets for the matter)?

Thanks.

Regards,
    Kok Hoor

Reply all
Reply to author
Forward
0 new messages