How to order by nested objects fields?

Andrew Medvedsky

I have some classes

class MarketProduct(models.Model, ObjectMarket):
    _state_class = 'MarketProductState'

    uuid = models.UUIDField(u'Код',
                        default=uuid.uuid4, editable=False)
    name = models.CharField(u'Название',
                        max_length=255, db_index=True)
class MarketItem(models.Model, ObjectMarket):
    _state_class = 'MarketItemState'

    STOCK, AUCTION = 1, 2
    ITEM_CHOICES = (
        (STOCK, u'Сток'),
        (AUCTION, u'Аукцион'),
    )

    product = models.ForeignKey(MarketProduct)
    start_at = models.DateTimeField(u'Начало продажи')

I want to get MarketItemViewSet and use

filter_backends = (filters.OrderingFilter,`)

I send request with filed orderby by angular. If I send orderby = start_at, all are good, but I want to send orderby = product.id, it doesn't work.

Aameer

As you are using django-rest-framework you have to use ordering_fields as you can see from the documentation here.Hope it helps example:

class UserListView(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (filters.OrderingFilter,)
    ordering_fields = ('username', 'email')
    ordering = ('created_on') # for reverse ordering = ('-created_on')

If an ordering attribute is set on the view, this will be used as the default ordering.

Typically you'd instead control this by setting order_by on the initial queryset, but using the ordering parameter on the view allows you to specify the ordering in a way that it can then be passed automatically as context to a rendered template

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to order nested fields with 'field_for' using rails

分類Dev

How to implement a form with nested fields?

分類Dev

How to loop over objects nested in objects in TypeScript

分類Dev

How to make an array of multiple nested objects?

分類Dev

How to delete all nested objects referenced to an object?

分類Dev

how to clear woocommerce checkout fields after place order?

分類Dev

How to implement onBlur/onFocus for a div with nested input fields?

分類Dev

How to get and set value in nested form fields in angular2?

分類Dev

How to destructure and initializing missing, nested fields in the same expression?

分類Dev

How to validate error messages for nested form group fields in angular

分類Dev

In Kibana how can you sum nested fields and then bucket for each document?

分類Dev

how to query nested array of objects by also filtering nested array of nested array in mongo

分類Dev

How do I create objects with enum types for fields

分類Dev

How to aggregate multiple objects with same values in certain fields?

分類Dev

How to sort a nested list by reverse length of the sublists and keep the original order

分類Dev

Firestore order by two fields

分類Dev

How to order 2 related tables in one query having order fields both of them

分類Dev

ngTable nested fields

分類Dev

Workfront sort nested fields

分類Dev

Android - How to retrieve list of objects in their insertion order from Firebase?

分類Dev

How to sort json objects using the order of another json

分類Dev

How to get all keys with values from nested objects

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

Flatten the deep nested list of objects and create a csv. How?

分類Dev

How to use reduce to retrieve values from deep nested array objects

分類Dev

How to parse multiple nested JSON objects/arrays with GSON?

分類Dev

ORDER BY results of a query (nested?)

分類Dev

Order NSArray with objects

分類Dev

How to flat array of objects which can contain nested objects in my case

Related 関連記事

  1. 1

    How to order nested fields with 'field_for' using rails

  2. 2

    How to implement a form with nested fields?

  3. 3

    How to loop over objects nested in objects in TypeScript

  4. 4

    How to make an array of multiple nested objects?

  5. 5

    How to delete all nested objects referenced to an object?

  6. 6

    how to clear woocommerce checkout fields after place order?

  7. 7

    How to implement onBlur/onFocus for a div with nested input fields?

  8. 8

    How to get and set value in nested form fields in angular2?

  9. 9

    How to destructure and initializing missing, nested fields in the same expression?

  10. 10

    How to validate error messages for nested form group fields in angular

  11. 11

    In Kibana how can you sum nested fields and then bucket for each document?

  12. 12

    how to query nested array of objects by also filtering nested array of nested array in mongo

  13. 13

    How do I create objects with enum types for fields

  14. 14

    How to aggregate multiple objects with same values in certain fields?

  15. 15

    How to sort a nested list by reverse length of the sublists and keep the original order

  16. 16

    Firestore order by two fields

  17. 17

    How to order 2 related tables in one query having order fields both of them

  18. 18

    ngTable nested fields

  19. 19

    Workfront sort nested fields

  20. 20

    Android - How to retrieve list of objects in their insertion order from Firebase?

  21. 21

    How to sort json objects using the order of another json

  22. 22

    How to get all keys with values from nested objects

  23. 23

    How to get the combination of array values from nested arrays in an array of objects

  24. 24

    Flatten the deep nested list of objects and create a csv. How?

  25. 25

    How to use reduce to retrieve values from deep nested array objects

  26. 26

    How to parse multiple nested JSON objects/arrays with GSON?

  27. 27

    ORDER BY results of a query (nested?)

  28. 28

    Order NSArray with objects

  29. 29

    How to flat array of objects which can contain nested objects in my case

ホットタグ

アーカイブ