【发布时间】:2010-10-24 07:16:39
【问题描述】:
我的 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是Search的一种。 FastaRun 扩展了搜索,因为为 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
因为Search 和FastaRun(因此FastaForm)是
交织在一起,我觉得我正在进入 Catch-22。我需要
保存一个Search 实例,其参数存储在 POST 中
请求,但必须使用FastaForm 验证其参数
验证。但是,我认为 FastaForm 不能被实例化,直到
我已经实例化了一个 Search 实例。然而,我无法实例化
Search 实例,直到我使用 FastaForm 验证...你得到
这个想法。
我在这里缺少什么?必须有一个相当干净的方法来做到这一点,但是 我看不清楚。
另外,如果我错了,请纠正我,但只要模型之间存在某种关系(例如,ForeignKey 和 ManyToMany 字段),任何时候都可能发生同样的依赖情况。因此,肯定有人想到了这一点。
【问题讨论】:
标签: django django-models django-forms dependencies