2011-03-29 66 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