【问题标题】:Dynamically populating values in djangoform在 djangoform 中动态填充值
【发布时间】:2011-03-22 01:04:42
【问题描述】:

问题详情: 1] 我的模型如下所示

class UserReportedData(db.Model):
    #country selected by the user, this will also populate the drop down list on the html page
    country = db.StringProperty( choices=['Afghanistan','Aring land'])
    #city selected by the user
    city = db.StringProperty()
    #date and time when the user reported the site to be down
    date = db.DateTimeProperty(auto_now_add=True)

2] 这个模型有一个国家,它是html页面中的一个下拉列表和城市,它是当前html页面中的一个文本字段

模型的形式如下:

class UserReportedDataForm(djangoforms.ModelForm):

    class Meta:
        #mechanism to get the users country and city
        geoiplocator_instance = GeoIpLocator()
        city_country_dictionary=geoiplocator_instance.get_country_city_dictionary()
        users_country_name = city_country_dictionary['country_name']
        users_city = city_country_dictionary['city']

        #using the model with the default country being users conutry and default city being users city
        model = UserReportedData(default={'country':users_country_name})

3] 类 geoiplocator 用于查找用户国家和城市。

问题:

1] 我希望国家/地区下拉列表显示变量“users_country_name”中的用户国家/地区 以及显示用户城市的城市文本字段,在变量“users_city”中

谢谢,

【问题讨论】:

    标签: python django google-app-engine django-forms


    【解决方案1】:

    你通常会通过覆盖__init__来做到这一点

    from django.forms import ModelForm, ChoiceField
    class MyModelForm(ModelForm):
        class Meta:
            model = MyModel
    
        def __init__(self, *args, **kwargs):
            super(MyModelForm, self).__init__(*args, **kwargs)
            geoiplocator_instance = GeoIpLocator()
            city_country_dictionary=geoiplocator_instance.get_country_city_dictionary()
            users_country_name = city_country_dictionary['country_name']
            users_city = city_country_dictionary['city']
    
            # not exactly sure what you wanted to do with this choice field.
            # make the country the only option? Pull a list of related countries?
            # add it and make it the default selected?
            self.fields['country'] = ChoiceField(choices = [(users_country_name, users_country_name),])
            self.fields['city'].initial = users_city
    

    【讨论】:

      【解决方案2】:

      您可以将表单类包装在一个函数中,然后在您的视图中调用这个函数。

      def make_user_reported_data_form(users_city, users_country_name):
          class UserReportedDataForm(djangoforms.ModelForm):
      
              class Meta:
                  #mechanism to get the users country and city
                  geoiplocator_instance = GeoIpLocator()
                  city_country_dictionary=geoiplocator_instance.get_country_city_dictionary()
                  users_country_name = city_country_dictionary['country_name']
                  users_city = users_city
                  model = UserReportedData(default={'country':users_country_name})
          return UserReportedDataForm
      

      【讨论】:

      • 谢谢@Spike,我今晚会尝试这个解决方案并发布结果。
      猜你喜欢
      • 2022-10-13
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2021-08-12
      • 2018-10-14
      相关资源
      最近更新 更多