简体   繁体   English

Django动态模型设计

[英]Django dynamic model design

I have a table that contains a dynamic number of entries, each entry contains data (like id, name, date) and also 3 radio buttons. 我有一个表,其中包含动态数量的条目,每个条目都包含数据(例如ID,名称,日期)以及3个单选按钮。

    class Entry(models.Model):
        id = ...
        name = ...
        data = ...
        selected_option = ...

I want to save all of the entries in the DB when the user submits the form. 我想在用户提交表单时将所有条目保存在数据库中。
I was wondering what would be the best way to design the model for this. 我想知道为此设计模型的最佳方法是什么。

I am not sure how your data looks like. 我不确定您的数据看起来如何。 It's not bad to store huge amount of data or structure your data. 存储大量数据或结构化数据也不错。 Let's say there are fixed number of options from which user will select the radio button right, you can store those options as choices in your db. 假设有固定数量的选项,用户可以从中选择单选按钮,您可以将这些选项作为选项存储在数据库中。

class Entry(models.Model):
    entry_id = ...
    name = ...
    date = ...

class SelectedOption(models.Model):
    class Options:
        OPTION_1 = 'O1'
        OPTION_2 = 'O2'
        Choices = (
           (OPTION_1, 'Option 1'),
           (OPTION_2, 'Option 2'),
        )

    option = CharField(choices=Options.Choices, max_length=2)
    entry = ForeignKey(Entry)

I think this way you won't have repetition of data. 我认为这样您就不会重复数据。 And your data will be more structured if you want to perform some analysis. 如果您想执行一些分析,则数据将更加结构化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM