是否可以指示 Django 根据其字段将模型实例保存到特定表中?

发布于 2024-09-26 21:00:44 字数 1055 浏览 3 评论 0原文

我正在尝试构建一个对现有表集进行建模的 Django 应用程序。这些表都具有相同的字段,以及每个表的自定义字段。我想要做的是对这个结构进行建模,并根据它们所附加的表模型将记录保存到特定的表中。

这些表可以经常创建,因此为每个表构建新模型是不可行的。

也许代码会更清楚地展示我想要做的事情:



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)

当保存具有“table_1”外键的新记录时,我希望最终操作遵循 insert into table_1 (. .fields..)values(..fieldvalues..)

这可能吗?我想我可以连接到信号或保存方法,但我想找到最简单的方法(如果存在)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-10-03 21:00:44

您可以动态创建非托管模型。您只需要创建一个将列名称映射到数据值的字典。完成后,您可以执行以下操作:

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)

将其包装在一个函数中,并将其放入 Record 上的 .save() 方法中。如果您有任何公共字段,可以将它们添加到 attrs 中,甚至更好:使用所有公共字段创建一个抽象模型,并在上面的最后一行继承它,而不是 models.Model

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文