2016-08-07 6 views
0

So versuche ich meine Lage Filter URL zu haben als api/v1/labels/?brand_location=Australia Marken zu filtern mit nur Australien Marken die Fehlermeldung zurück, sondern halten Empfang:Ungültige Ressourcensuche Daten zur Verfügung gestellt (nicht übereinstimmen Typ)

{"error": "Invalid resource lookup data provided (mismatched type)."} 

Aber wenn Gebrauch api/v1/labels/?brand_location/=Australia funktioniert, aber doest Filter nur Australien Standorte, gibt es die vollständige Antwort nicht Standorte ausgeschlossen.

Also meine Fragen sind:

  1. Wie kann ich den Slash entfernen? Und lasst es nur Australien filtern
  2. Ist das der richtige Weg? Bei Verwendung von Fremdschlüsseln mit Django Tastypie?

Mein Code unten:

Models.py

class Brand(models.Model): 

    brand_location = models.ForeignKey('Location', null=True, blank=True, default="") 


class Location(models.Model): 

    state_or_country = models.CharField(unique=True, max_length=200, blank=True, default="", verbose_name=_('Location'), 

api.py

class LocationResource(ModelResource): 
    class Meta: 
     excludes = ['modified', 'id', 'created'] 

     queryset = Location.objects.all() 

     resource_name = 'locations' 

class LabelResource(ModelResource): 

    brand_location = fields.ForeignKey(LocationResource, 'brand_location', full=True) 

    class Meta: 

     filtering = { 
      "brand_location": ALL 
     } 

     queryset = Brand.objects.all() 

     resource_name = 'labels' 

Snippet JSON Antwort

{ 
    "labels": [ 
    { 
     "brand_location": { 
     "state_or_country": "Australia" 
     } 
    } 
    ], 
    "meta": { 
    "limit": 6, 
    "next": "/unlabel-network/unlabel-network-api/v1/labels/?limit=6&brand_location%2F=Australia&offset=6", 
    "offset": 0, 
    "previous": null, 
    "total_count": 128 
    } 
} 

Antwort

0

api/v1/labels/?brand_location=Australia sucht nach Location.id=Australia.

erlauben tiefere Filterung:

filtering = { 
    "brand_location": ALL_WITH_RELATIONS 
} 

und sucht state_or_country Feld:

api/v1/labels/?brand_location__state_or_country=Australia 
+0

Dies ist das neue Fehler, den ich erhalte, wenn ich die oben angegebene URL benutze: {"error": "Suchvorgänge sind nicht mehr als eine Ebene tief im Feld 'brand_location' erlaubt."} – Amechi

0

Ich brauchte nur filtering meiner LocationResource Dann ALL_WITH_RELATIONS dict auf meinem LabelResource

meiner Filterung hinzufügen zu hinzufügen
class LocationResource(ModelResource): 
    class Meta: 
     excludes = ['modified', 'id', 'created'] 

     queryset = Location.objects.all() 

     resource_name = 'locations' 

     filtering = { 
      "state_or_country": ALL 
     } 

class LabelResource(ModelResource): 

    brand_location = fields.ForeignKey(LocationResource, 'brand_location', full=True) 

    class Meta: 

     filtering = { 
      "brand_location": ALL, 
      "brand_location": ALL_WITH_RELATIONS 
     } 

     queryset = Brand.objects.all() 

     resource_name = 'labels' 
Verwandte Themen