我有兩個型號在我的Django應用程序,用於儲存一定的同源性搜索程序搜索參數的目的:這裏ModelForms在Django,其中底層模型依賴於其他模型(通過OneToOneField)
# 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
定義了更多參數。 A FastaRun
必須有一個與其關聯的實例Search
,並且此實例是FastaRun
的主鍵。
對於FastaRun
類,我有ModelForm
。
# 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
)的 交織在一起,我覺得我陷入左右爲難。我需要 保存一個Search
實例,其參數存儲在POST 請求中,但其參數必須使用FastaForm
的 驗證驗證。但是,我認爲FastaForm
不能實例化,直到 我已經實例化了一個Search
實例。然而,我不能實例化一個實例,直到我使用FastaForm
進行驗證...您得到了 這個想法。
我在這裏錯過了什麼?必須有一個相當乾淨的方式來做到這一點,但我不明白看到它。
此外,糾正我,如果我錯了,但是這種相同的依賴情況可能發生在任何時候你有模型之間的某種關係(例如,也爲ForeignKey
和ManyToMany
字段)。因此,有人肯定會明白這一點。
程序是一個CharField,用於標識執行搜索的程序(例如,,'fasta35'或'ssearch35'或'blast')。 'fasta35'和'ssearch35'必須保存爲FastaRun實例。這應該足夠作爲一個過濾器,對嗎?有沒有辦法從Search實例到它相應的FastaRun實例(我們可能會認爲它有一個)? – gotgenes 2009-05-08 19:20:23