2013-02-27 26 views
6

मैं डीजेगो रेस्ट फ्रेमवर्क का उपयोग कर रहा हूं, मैंने एपीआई के वेब ब्राउज़ करने योग्य भाग पर ध्यान दिया है, क्लिक किए जाने पर 'विकल्प' नामक एक बटन है जो इसे दिखाता है ...Django Rest Framework विकल्पों में फ़िल्टर और ऑर्डरिंग दिखाएं

HTTP 200 OK Vary: Accept Content-Type: text/html Allow: HEAD, GET, OPTIONS 
{ 
    "parses": [ 
     "application/json", 
     "application/x-www-form-urlencoded", 
     "multipart/form-data" 
    ], 
    "renders": [ 
     "application/json", 
     "text/html" 
    ], 
    "name": "Products", 
    "description": "API endpoint." 
} 

मेरा सवाल यह है कि, क्या मैं वैसे भी सभी फ़िल्टर विकल्पों को इस यूआरएल के लिए अन्य सामान सूचीबद्ध कर सकता हूं?

उत्तर

6

आप OPTIONS देख सकते हैं जो भी आप चाहते हैं, .metadata() विधि को ओवरराइड करके।

यहाँ देखें: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/views.py#L340


के रूप में अद्यतन: http://www.django-rest-framework.org/api-guide/metadata/

+0

क्या आप बेहतर व्याख्या कर सकते हैं कि मैं कैसे देख सकता हूं .metadata() को देखने पर? यह लिंक 'लाइन 340' पर इंगित करता है लेकिन मुझे नहीं पता कि आप किस विधि का संदर्भ लेते हैं। – ePascoal

+1

ऊपर मेरा अपडेट देखें। –

0

आप पूरी तरह से ऐसा कर सकते हैं: हम अब एक अनुकूलन मेटाडाटा एपीआई इस आसान बना देता है कि है। यहां एक custom metadata class है जिसे मैं यहां स्टैक ओवरफ्लो पर अद्यतित रख रहा हूं। यह बस सभी उपलब्ध फ़िल्टर, उनके प्रकार, और उनके विकल्पों की सूची देता है। यह भी आदेश देने के क्षेत्रों है कि एक वर्ग पर उपलब्ध हैं सूचीबद्ध करता है:

class SimpleMetadataWithFilters(SimpleMetadata): 

    def determine_metadata(self, request, view): 
     metadata = super(SimpleMetadataWithFilters, self).determine_metadata(request, view) 
     filters = OrderedDict() 
     if not hasattr(view, 'filter_class'): 
      # This is the API Root, which is not filtered. 
      return metadata 

     for filter_name, filter_type in view.filter_class.base_filters.items(): 
      filter_parts = filter_name.split('__') 
      filter_name = filter_parts[0] 
      attrs = OrderedDict() 

      # Type 
      attrs['type'] = filter_type.__class__.__name__ 

      # Lookup fields 
      if len(filter_parts) > 1: 
       # Has a lookup type (__gt, __lt, etc.) 
       lookup_type = filter_parts[1] 
       if filters.get(filter_name) is not None: 
        # We've done a filter with this name previously, just 
        # append the value. 
        attrs['lookup_types'] = filters[filter_name]['lookup_types'] 
        attrs['lookup_types'].append(lookup_type) 
       else: 
        attrs['lookup_types'] = [lookup_type] 
      else: 
       # Exact match or RelatedFilter 
       if isinstance(filter_type, RelatedFilter): 
        model_name = (filter_type.filterset.Meta.model. 
            _meta.verbose_name_plural.title()) 
        attrs['lookup_types'] = "See available filters for '%s'" % \ 
              model_name 
       else: 
        attrs['lookup_types'] = ['exact'] 

      # Do choices 
      choices = filter_type.extra.get('choices', False) 
      if choices: 
       attrs['choices'] = [ 
        { 
         'value': choice_value, 
         'display_name': force_text(choice_name, strings_only=True) 
        } 
        for choice_value, choice_name in choices 
       ] 

      # Wrap up. 
      filters[filter_name] = attrs 

     metadata['filters'] = filters 

     if hasattr(view, 'ordering_fields'): 
      metadata['ordering'] = view.ordering_fields 
     return metadata 

रखें कि अपनी परियोजना में कहीं न कहीं, तो अपने DEFAULT_METADATA_CLASS निर्धारित करते हैं, और यदि आप ऐसा तरह अपने OPTIONS अनुरोध पर नई कुंजी के साथ पूरी तरह से तैयार, किया जाना चाहिए:

"filters": { 
    "sub_opinions": { 
     "type": "RelatedFilter" 
    }, 
    "source": { 
     "type": "MultipleChoiceFilter", 
     "choices": [ 
      { 
       "display_name": "court website", 
       "value": "C" 
      }, 
     ] 
    } 
    ...more... 
} 

यह भी choices प्रदर्शित करते हैं, जिस तरह से यह डीआरएफ में कहीं संभाला है मिरर होगा।

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