2017-11-22 3 views
0

Ich habe ein Modell mit ForeignField namens typeofingredient.Django Rest Framework: Bestellung funktioniert nicht richtig

Wenn ich versuche, die folgende Serializer Klasse

class IngredientListSerializer(ModelSerializer): 
    class Meta: 
     model = Ingredient 
     fields = '__all__' 

und dann versuche ich http://localhost:8000/api/ingredients/?ordering=typeofingredient

Die Ergebnisse aller w.r.t auf die ID von typeofingredient sortiert werden.

Später habe ich das Namensfeld von typeofingredient anstelle von id in api angezeigt. SO i die Serializer-Klasse unten geändert:

class IngredientListSerializer(ModelSerializer): 
    typeofingredient = ReadOnlyField(source='typeofingredient.name') 
    class Meta: 
     model = Ingredient 
     fields = '__all__' 

und dann versuche ich http://localhost:8000/api/ingredients/?ordering=typeofingredient, nicht um es nicht die überhaupt Ergebnisse.

wie zu lösen dieses

Antwort

0

ich die Antwort

https://github.com/encode/django-rest-framework/issues/1005 und https://github.com/encode/django-rest-framework/pull/5533

Also ich meine djangrestframework aktualisieren gefunden haben:

$ pip install djangorestframework -U 
Collecting djangorestframework 
    Downloading djangorestframework-3.7.3-py2.py3-none-any.whl (1.5MB) 
    100% |████████████████████████████████| 1.5MB 447kB/s 
Installing collected packages: djangorestframework 
    Found existing installation: djangorestframework 3.3.3 
    Uninstalling djangorestframework-3.3.3: 
     Successfully uninstalled djangorestframework-3.3.3 
Successfully installed djangorestframework-3.7.3 

Dann funktionierte alles gut

class IngredientListSerializer(ModelSerializer): 
    typeofingredient = ReadOnlyField(source='typeofingredient.name',default=None) 
    class Meta: 
     model = Ingredient 
     fields = '__all__ 

http://localhost:8000/api/ingredients/?ordering=-typeofingredient und http://localhost:8000/api/ingredients/?ordering=typeofingredient

funktioniert gut '

Verwandte Themen