2011-04-06 5 views
5
करने के लिए एक पर कार्यक्षमता डालने का उपयोग

एक] सारांश:मिलता है या कई Django मॉडल और modelforms

मिलता है या कई Django मॉडल के लिए एक पर कार्यक्षमता डालें, जाँच एक रिकार्ड या संबंध एक नया जोड़ने से पहले मौजूद है या नहीं का उपयोग करना एक।

बी] विवरण:

1] मैं निम्नलिखित Django मॉडल संरचना सिटी (कई) के लिए देश (एक) है

2] मैं html पृष्ठ पर प्रपत्र प्रदर्शित करने के लिए उपयोग कर रहा हूँ ModelForm । फॉर्म टेम्पलेट मानों के रूप में पारित किए जाते हैं और टेम्पलेट मानों को HTML पृष्ठ

3] जब पोस्ट अनुरोध आता है, तो कोड देश और शहर के रूप में डेटा निकालता है, वैधता के लिए जांच करता है और इसे बचाता है।

1] मैं अपने देश और शहर रिश्ते के साथ get_or_insert कार्यक्षमता का उपयोग करने

2] असल में, मैं वहाँ है कि क्या जाँच करना चाहते हैं:

सी] जारी करना/आइटम मैं मदद के साथ की जरूरत है उपयोगकर्ता द्वारा चयनित देश के लिए डेटाबेस में मौजूदा देश रिकॉर्ड,

2.1] यदि देश मौजूद है, तो जांच करें कि चयनित देश के लिए कोई शहर रिकॉर्ड है या नहीं।

2.1.1] अगर शहर रिकॉर्ड मौजूद है, हम अच्छा कर रहे हैं, एक और रिकार्ड

2.1.2] अगर शहर रिकॉर्ड मौजूद नहीं है, शहर रिकॉर्ड बनाने के जोड़ सकते हैं और साथ देश संबद्ध करने के लिए कोई जरूरत नहीं यह

2.2] यदि देश मौजूद नहीं है, तो देश का रिकॉर्ड बनाएं, शहर का रिकॉर्ड बनाएं और देश के रिकॉर्ड के साथ शहर के रिकॉर्ड को संबद्ध करें।

सी] कोड अंशः

1] मॉडल और रूपों -

class UserReportedCountry(db.Model): 
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 UserCountryForm(djangoforms.ModelForm): 
    class Meta: 
     model = UserReportedCountry 

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

2] कोड प्रिंट कि forms--

user_country_form और user_city_form हैं पाइथन कोड से टेम्पलेट मानों के रूप में पारित

__TEMPLATE_USER_COUNTRY_FORM = 'user_country_form' 
__TEMPLATE_USER_CITY_FORM = 'user_city_form' 

def get(self): 
    template_values = { 
     self.__TEMPLATE_USER_COUNTRY_FORM: UserCountryForm(), 
     self.__TEMPLATE_USER_CITY_FORM: UserCityForm() 
    } 

    #rendering the html page and passing the template_values 
    self.response.out.write(template.render(self.__MAIN_HTML_PAGE, template_values)) 

3] एचटीएमएल कोड अंश जो html पृष्ठ पर टेम्पलेट मूल्यों को प्रदर्शित करता है

<div id="userDataForm"> 
<form method="POST" action="/UserReporting"> 
<table> 
<!-- Printing the forms for users country, city --> 
    {{ user_country_form }} 
    {{ user_city_form }}  
</table> 
<input type="submit" name="submit" value= "submit"> 
</form> 
</div> 

डेटा को बचाने के लिए 3] कोड अंश - पढ़ने के लिए

def post(self): 
    self.store_user_data() 
    self.redirect('/') 

#method to save users data in the models UserReportedCountry, UserReportedCity 
def store_user_data(self): 
    #getting user's country and city 
    user_reported_country = UserCountryForm(self.request.POST) 
    user_reported_city = UserCityForm(self.request.POST) 

    if (user_reported_country.is_valid() and user_reported_city.is_valid()): 
     #save the country data 
     country_entity = user_reported_country.save() 
     #save the city data, and relate county with city 
     city_entity = user_reported_city.save(commit=False) 
     city_entity.country = country_entity 
     city_entity.put() 
     return 
    else: 
     return 

ठनक ।

[# संपादित करें 1]

@ टॉरस्टेन, पोस्ट मुझे get_or_insert के लिए सूचक दिया था, मैं तो StackOverflow पद भर में (http://stackoverflow.com/questions/4812832/get-or-create-matching आया

def store_user_data(self): 
    #getting user's country and city 
    user_reported_country = UserCountryForm(self.request.POST) 
    user_reported_city = UserCityForm(self.request.POST) 

    if (user_reported_country.is_valid() and user_reported_city.is_valid()): 
     #save the country and city data 
     country_record = self.store_country_data() 
     city_record = self.store_city_data(country_record) 
     return 
    else: 
     return 

def store_country_data(self): 
    user_reported_country_name = self.request.POST['country_name'] 
    key_name = self.sanitize_key_name(user_reported_country_name) 
    country_entity = UserReportedCountry.get_or_insert(key_name, country_name = user_reported_country_name) 
    country_entity.put() 
    return country_entity 

def store_city_data(self, country_record): 
    user_reported_city_name = self.request.POST['city_name'] 
    key_name = self.sanitize_key_name("%s:%s" % (country_record.country_name, str(user_reported_city_name))) 
    city_entity = UserReportedCity.get_or_insert(key_name, 
               city_name = user_reported_city_name, 
               country= country_record) 

    city_entity.put() 
    return city_entity 

def sanitize_key_name(self,key_name): 
    return re.sub(r'\s', '', key_name.lower()) 

अनुरोध:: किसी एक त्वरित कोड कृपया कर सकते हैं -कुंजी और उपयोगकर्ता के अजगर, एप्लिकेशन के इंजन) और रिकॉर्ड को बचाने के लिए उपयोगकर्ता get_or_insert

संहिता के KEY_NAME कार्यक्षमता लागू इस प्रकार की तरह दिखता है समीक्षा करें और मुझे बताएं कि क्या मैं कुछ गलत कर रहा हूं या बना रहा हूं ओमे रूकी गलतियों?

+0

सभी। क्या पूछते हैं? आप क्या परेशानी कर रहे हैं? –

+0

क्षमा करें @ निक जॉनसन, स्पष्ट नहीं होने के लिए। अनुभाग का नाम "समस्या/वस्तु" में मदद की ज़रूरत है, क्योंकि प्रश्न बहुत विस्तृत था। – bhavesh

उत्तर

0

उपयोग get_or_create (अपने get_or_insert के बराबर):

country, country_is_new = UserReportedCountry.objects.get_or_create(country_name=my_country_name) 
city, city_is_new = UserReportedCity.objects.get_or_create(country=country, city_name=my_city_name) 
चीजें आप सूची के रूप में सवालों के बयान कर रहे हैं की
+0

@ टोरस्टन, आपकी प्रतिक्रिया के लिए धन्यवाद। मैंने ऐप इंजन एपीआई की खोज की और पाया कि get_or_create Google ऐप इंजन के साथ समर्थित नहीं है। – bhavesh

+0

@ टोरस्टन, आपकी पोस्ट ने मुझे get_or_insert कार्यक्षमता के लिए एक सूचक दिया और मैं stackoverflow पर कुंजी नाम पोस्ट में आया (http://stackoverflow.com/questions/4812832/get-or-create-matching-key-and-user- पायथन-एप-इंजन) और मेरे मूल पोस्ट के "EDIT # 1" अनुभाग में इंगित कोड को कार्यान्वित किया। क्या आप कृपया कोड समीक्षा कर सकते हैं और मुझे बताएं कि क्या मैं कोई गलती कर रहा हूं? – bhavesh

+0

@lance_klusener, आपने उल्लेख नहीं किया है कि आप Django के बजाय ऐप इंजन मॉडल और उनके क्वेरी एपीआई का उपयोग करना चाहते हैं। मैं मान रहा था कि आप केवल Django के बारे में बात करते हैं। हालांकि मुझे इसे अपने मॉडल के अनुसार देखना चाहिए था। : पी –

संबंधित मुद्दे