2011-12-20 18 views
5

मेरे पास एक सामान्य कार्य है जो किसी दिए गए ऑब्जेक्ट के _meta.fields पर पुनरावृत्त करता है। ManyToMany फ़ील्ड को छोड़कर सभी फ़ील्ड नाम और मान सही तरीके से लाए जाते हैं। ऐसा लगता है कि कई लोग कई क्षेत्रों को अनदेखा करते हैं। हम m2m फ़ील्ड से fks को कैसे पुनः प्राप्त कर सकते हैं?कई मेट्रोमेनी फ़ील्ड _meta.fields में दिखाई नहीं दे रहे हैं

def myfunc(self) 
    for field in self._meta.fields: 
     type = field.get_internal_type() 
     name = field.name 
     val = getattr(self,field.name) 

उत्तर

0

में हैं यदि आप एक मॉडल में सभी फ़ील्ड नाम प्राप्त करना चाहते हैं। आपको self._meta.many_to_many + self._meta.fields का उपयोग करने की आवश्यकता नहीं है।

आप केवल [field.name for field in model._meta.get_fields()] का उपयोग कर सकते हैं।

ध्यान दें कि get_fields सभी क्षेत्रों वापस आ जाएगी (सहित कई-कई लोगों के लिए और विदेशी कुंजी)

Django get_fields:

def get_fields(self, include_parents=True, include_hidden=False): 
    """ 
    Returns a list of fields associated to the model. By default, includes 
    forward and reverse fields, fields derived from inheritance, but not 
    hidden fields. The returned fields can be changed using the parameters: 

    - include_parents: include fields derived from inheritance 
    - include_hidden: include fields that have a related_name that 
         starts with a "+" 
    """ 
संबंधित मुद्दे

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