2011-03-29 64 views
0

摘要: 我有1款的ModelForm不打印一個領域的許多關係

之間有很多的層次關係

國家(1) - >城(許多)

市(1) - - >狀態(很多)

我有一個表單,假設打印屬於所有這些模型的字段,但是當我打印時,我只能看到「城市」字段,而且它也顯示爲下拉列表作爲文本框出現。我試圖尋找這個問題,但沒有解決方案出現。

代碼摘錄:

from google.appengine.ext import db 
from google.appengine.ext.db import djangoforms 

class UserReportedCountry(db.Model): 
    #country selected by the user 
    country_name = db.StringProperty(required=True, 
          choices=['Afghanistan','Aring land Islands'] 
         ) 

class UserReportedCity(db.Model): 
    country = db.ReferenceProperty(UserReportedCountry, collection_name='cities') 
    city_name = db.StringProperty(required=True) 

class UserReportedStatus(db.Model): 
    city = db.ReferenceProperty(UserReportedCity, collection_name='statuses') 
    status = db.BooleanProperty(required=True) 
    date_time = db.DateTimeProperty(auto_now_add=True) 


class UserReportedDataForm(djangoforms.ModelForm):  
    class Meta: 
    model = UserReportedStatus 
    exclude = ('status’) 

感謝,

[編輯#1]

我無意間看到這個帖子(how to make dynamically generated forms with one to many relationships in django)來了,跟着的是,提交有方法用於打印頁面上的表格

A]形式模型中的類現在

class UserCountryForm(djangoforms.ModelForm): 
    class Meta: 
    model = UserReportedCountry 

class UserCityForm(djangoforms.ModelForm): 
    class Meta: 
    model = UserReportedCity 
    exclude = ('country',) 

class UserStatusForm(djangoforms.ModelForm): 
    class Meta: 
    model = UserReportedStatus 
    #hiding the site_is_up property 
    exclude = ('site_is_up', 'city') 

B]的方法,打印這些形式:

def print_user_reporting_form(self): 
    self.response_variable.out.write('<div id="userDataForm">' 
             '<form method="POST" ' 
               'action="/UserReporting">' 
              '<table>') 

    #method call to print the pre-populated user form with users country and city value 
    self.response_variable.out.write (UserCountryForm()) 
    self.response_variable.out.write (UserCityForm()) 
    self.response_variable.out.write(UserStatusForm()) 

    self.response_variable.out.write (  '</table>' 
              '<input type="submit" name="report_up" value= "Report Up">' 
              '<input type="submit" name="report_down" value= "Report Down">' 
             '</form>' 
             '</div>') 

謝謝,

回答

1

你的形式被正確地輸出。

你在城市和狀態之間有一對多關係。

       country 
      __ __ __ __ __ __ __ __ | __ __ __ __ __ __ __ 
     /    |    |    \ 
     city    city   city   city 
    __ _|_ __  __ _|_ __  __ _|_ __  __ _|_ __ 
    | | | |  | | | |  | | | |  | | | | 
    s s s s  s s s s  s s s s  s s s s  

您有創建一個單一的地位和城市之間的聯繫形式,你可以做到這一點一再創造一個城市到多個狀態的關係。

下拉式選項是詢問您希望將哪個城市與您的狀態關聯。

你明確地排除的狀態字段,並採取this

當前實現,設置 auto_now或auto_now_add爲True將 導致該字段可編輯的=假 和空白筆記=真集。

+0

感謝@kriegar爲您的快速反應。 「UserReportedDataForm」中的「狀態」是有意排除的,因爲我不希望該字段被暴露給用戶,有2個按鈕的html形式的邏輯,並且取決於所選的按鈕,該標誌將被設置爲真或錯誤。我不想讓「date_time」暴露給用戶。 我想在表格中打印「country_name」和「city_name」。正如你可以從我的模型聲明中看到的,「country_name」應該是一個下拉列表,「city_name」應該是一個文本框。有沒有辦法做到這一點? – bhavesh 2011-03-29 09:50:31

+0

您是否指city_name字段的文本輸入?或文本區域?我想我明白你在問什麼。你需要一個基於用戶選擇的國家選擇的動態填充city_name字段。沒有ajax,這是不可能的。 – DTing 2011-03-29 09:56:49

+0

我來到了鏈接(http://stackoverflow.com/questions/3122962/how-to-make-dynamically-generated-forms-with-one-to-many-relationships-in-django),並遵循方法用戶曾經用於打印一到多個模型的表單。你能否在我的主要提交中檢查[編輯#1],看看我是否正確地做了這件事? – bhavesh 2011-03-29 10:50:15