I have a list in my application which is created from two related tables(products and stocks). I want to order items by date which is in the Stocks table. My API returns well ordered list but in my app the list is not ordered. When I try to order by any of Products fields it works fine - also in react.
My view:
class StocksView(viewsets.ModelViewSet):
serializer_class = StocksSerializer
ordering_fields = ['date']
filter_backends = (filters.OrderingFilter,)
def get_queryset(self):
queryset = Stocks.objects.all().order_by('date')
return queryset
def perform_create(self, serializer):
serializer.save(user=self.request.user)
Method from React:
const fetchStocks = async () => {
try {
const response = await axiosInstance.get('/stocks/?ordering=date')
if (response) {
setStocksInfo(response.data)
console.log(response.data)
}
} catch (error) {
throw error;
}
}
And returned list from two merged tables:
{productsInfo.map(products => {
const product = stocksInfo.filter(e => e.product_id === products.product_id);
return (
product.map((stock, i) => (
<tr key={i} className="table-data">
<td className="col-3"> {products.name}</td>
<td className="col-3">{stock.date}</td>
<td className="col-1">{stock.count}</td>
</tr>
))
)
})}