开发者

Dynamically populating values in djangoform

Problem details: 1] I have a model that looks like follows

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] This model has a country, which is a drop down list in the html page and city which is a text field currently in the html page

The form for the model looks like follows:

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] the class geoiplocator is used for finding users country and city.

Questions:

1] I want the country drop down list to show the users country which is in variable "users_country_name" and the city text field to show users city, which is in varialble "users_city"

thanks,


You would normally do this by overriding __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


You can wrap the form class inside a function, and then in your view just call this function.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜