Django中的ModelForms,底层模型依赖于另一个模型(通过OneToOneField)

时间:2009-05-06 19:50:12

标签: django django-models django-forms dependencies

我的Django应用程序中有两个模型,用于存储用于某些同源搜索程序的搜索参数:

# models.py
class Search(models.Model):
    """A class to represent search runs."""

    program = models.CharField(max_length=20)
    results_file = models.FileField(
        upload_to=(SEARCH_RESULTS_DIR)
    )
    timestamp = models.DateTimeField()

    def __unicode__(self):
        return u'%s %s' % (self.program, self.timestamp)


class FastaRun(models.Model):

    search = models.OneToOneField('Search', primary_key=True)
    # the user-input FASTA formatted protein sequence
    query_seq = models.TextField()
    # -b "Number of sequence scores to be shown on output."
    number_sequences = models.PositiveIntegerField(blank=True)
    # -E "Limit the number of scores and alignments shown based on the
    # expected number of scores." Overrides the expectation value.
    highest_e_value = models.FloatField(default=10.0,
            blank=True)
    # -F "Limit the number of scores and alignments shown based on the
    # expected number of scores." Sets the highest E-value shown.
    lowest_e_value = models.FloatField(blank=True)
    mfoptions = [
            ('P250', 'PAM250'),
            ('P120', 'PAM120'),
            ('BL50', 'BLOSUM50'),
            ('BL62', 'BLOSUM62'),
            ('BL80', 'BLOSUM80')
    ]
    matrix_file = models.CharField(
            max_length=4,
            choices=mfoptions,
            default='BL50'
    )
    database_option = models.CharField(
            max_length=25,
            choices=BLAST_DBS,
            default=INITIAL_DB_CHOICE
    )
    ktupoptions = [(1, 1), (2, 2)]
    ktup = models.PositiveIntegerField(
            choices=ktupoptions,
            default=2,
            blank=True
    )

请注意,FastaRun是一种SearchFastaRun扩展了搜索范围,因为为FastaRun定义了更多参数。 FastaRun必须与Search实例相关联,此Search实例是FastaRun的主键。

ModelForm班有FastaRun

# views.py
class FastaForm(forms.ModelForm):

    class Meta:
        model = models.FastaRun

我有一个视图函数,我需要使用它来填充FastaForm并根据用户提交的表单保存新的Search实例和新的FastaRun实例。表单包含选择Search实例的选项。这是不可能的,因为Search实例只有在用户实际提交此搜索后才能存在。

以下是该功能需要做的概述:

# also in views.py
def fasta(request, ...):
    # populate a FastaForm from the information POSTed by the user--but
    # how to do this when there's no Search information coming in from
    # the user's request. We need to create that Search instance, too,
    # but we also have to...

    # validate the FastaForm
    # ... before we can ...

    # create a Search instance and save() it

    # use this saved Search instance and give it to the FastaForm [how?]

    # save() the FastaForm [save the world]

    pass

因为SearchFastaRun(因此FastaForm)是 交织在一起,我觉得我正在进入一个Catch-22。我需要 保存Search实例,其参数存储在POST中 请求,但必须使用FastaForm验证其参数 验证。但是,我认为FastaForm直到无法实例化 我已经实例化了一个Search实例。然而,我无法实例化 Search实例直到我使用FastaForm进行验证...你得到了 这个想法。

我在这里缺少什么?必须有一个相当干净的方法来做到这一点,但是 我看不清楚。

另外,如果我错了,请纠正我,但是只要您在模型之间存在某种关系(例如,ForeignKeyManyToMany字段),也会出现相同的依赖情况。因此,有人肯定会想到这一点。

1 个答案:

答案 0 :(得分:2)

这是我将使用继承来处理这个问题的情况:

# models.py
class Search(models.Model):
    """A class to represent search runs."""
    ...

class FastaRun(Search):
    # one-to-one field has been removed
    ....

现在,根据定义实例化FastaRun也会实例化Search。 Django也可以通过为FastaRun设置一个单独的表格来Search来正确处理数据库方面的问题。您的验证应该与表单一起按预期工作。如果您要对Search对象进行任何查询,那么您可能想要添加的唯一事情就是向所有子类重写的Search添加一个类型字段,以便您可以过滤这些结果。

相关问题