2009-05-06 36 views
4

我有兩個型號在我的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是一種SearchFastaRun擴展了搜索範圍,爲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 

因爲SearchFastaRun(因此FastaForm)的 交織在一起,我覺得我陷入左右爲難。我需要 保存一個Search實例,其參數存儲在POST 請求中,但其參數必須使用FastaForm的 驗證驗證。但是,我認爲FastaForm不能實例化,直到 我已經實例化了一個Search實例。然而,我不能實例化一個實例,直到我使用FastaForm進行驗證...您得到了 這個想法。

我在這裏錯過了什麼?必須有一個相當乾淨的方式來做到這一點,但我不明白看到它。

此外,糾正我,如果我錯了,但是這種相同的依賴情況可能發生在任何時候你有模型之間的某種關係(例如,也爲ForeignKeyManyToMany字段)。因此,有人肯定會明白這一點。

回答

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,該類型字段被所有子類覆蓋,因此您可以過濾掉這些結果。

+0

程序是一個CharField,用於標識執行搜索的程序(例如,,'fasta35'或'ssearch35'或'blast')。 'fasta35'和'ssearch35'必須保存爲FastaRun實例。這應該足夠作爲一個過濾器,對嗎?有沒有辦法從Search實例到它相應的FastaRun實例(我們可能會認爲它有一個)? – gotgenes 2009-05-08 19:20:23

相關問題