是否可以指示 Django 根据其字段将模型实例保存到特定表中?
我正在尝试构建一个对现有表集进行建模的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以动态创建非托管模型。您只需要创建一个将列名称映射到数据值的字典。完成后,您可以执行以下操作:
将其包装在一个函数中,并将其放入
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:
Wrap this up in a function, and put it in your
.save()
method onRecord
. If you have any common fields, you can add them toattrs
, or even better: create an abstract model with all the common fields and inherit that in the last line above instead ofmodels.Model
.