Modelform not printing fields of one to many relationships
Summary: I have 1 to many hierarchical relationships between models
Country (1) --> City (Many)
City (1) --> Status (Many)
I have a form that is suppose to print the fields belonging to all these models, but when I print I only see the “city” field and that too appears as a drop-down list instead of appearing as a textbox. I tried searching for this problem, but no solutions appeared.
Code excerpt:
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.StringPro开发者_如何学JAVAperty( 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’ )
Thanks,
[EDIT#1]
I accidentally came across this post (how to make dynamically generated forms with one to many relationships in django) and followed the method that the submitter had used regarding printing forms on the page
A] Forms in the model class now
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] Method that prints these forms:
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>')
Thanks,
Your form is outputting correctly.
You have a one to many relations between cities and statuses.
country
__ __ __ __ __ __ __ __ | __ __ __ __ __ __ __
/ | | \
city city city city
__ _|_ __ __ _|_ __ __ _|_ __ __ _|_ __
| | | | | | | | | | | | | | | |
s s s s s s s s s s s s s s s s
You have a form that is creating the link between a single status and a city, you can do this repeatedly to create a one city to many statuses relationship.
The drop down is asking which city you wish to associate to your status.
you explicity excluded the status field and take note of this
Note
As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.
精彩评论