Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219310
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:37:50+00:00 2026-05-17T15:37:50+00:00

I’m attempting to construct a Django application that models an existing set of tables.

  • 0

I’m attempting to construct a Django application that models an existing set of tables. These tables all have the same fields, plus custom fields per table. What I’m wanting to do is model this structure, and have records save to a particular table based on what table model they are attached to.

These tables can be created quite often, so it is unfeasible to construct new models per table.

Perhaps the code will demonstrate what I’m trying to do more clearly:



class CustomField(models.Model):
    column_name = models.CharField(max_length=100)
    description = models.CharField(max_length=255, blank=True, null=True)

class CustomData(models.Model):
    custom_field = models.ForeignKey(CustomField)
    value = models.CharField(max_length=100, blank=True, null=True)
    # value will always be a nullable varchar(100)

class Table(models.Model):
    table_name = models.CharField(max_length=255)
    name = models.CharField(max_length=100)
    custom_fields = models.ManyToManyField(CustomField)

class Record(models.Model):
    table = models.ForeignKey(Table)
    ... list of common fields omitted ...
    custom_values = models.ManyToManyField(CustomData)

When saving a new record that has a foreign key to ‘table_1’, I would like the eventual operation to be along the lines of insert into table_1 (..fields..) values (..field values..)

Is this possible? I guess I could hook into signals or the save method, but I’d like to find the simplest approach if such exists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:37:51+00:00Added an answer on May 17, 2026 at 3:37 pm

    You can create unmanaged models dynamically. You just need to create a dict mapping column names to the data values. Once you have that, you can do the following:

    from django.db import models
    
    # This is the dict you created, mapping column names to values
    values = {col_1: value_1, col_2: value_2, col_3: value_3, ... }
    
    # Create a dict defining the custom field types, eg {col_name: django_field}
    attrs = dict((c, models.CharField(max_length=100, blank=True, null=True)) for c in values)
    
    # Add a Meta class to define the table name, eg table_1
    class Meta:
      app_label = myapp
      db_table = 'table_1'
      managed = False
    attrs['Meta'] = Meta
    attrs['__module__'] = 'path.to.your.apps.module'
    
    DynamicModel = type('MyModel', (models.Model,), attrs)
    
    # Save your data
    DynamicModel.objects.create(**values)
    

    Wrap this up in a function, and put it in your .save() method on Record. If you have any common fields, you can add them to attrs, or even better: create an abstract model with all the common fields and inherit that in the last line above instead of models.Model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.