You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to sort the results from the django rest framework api, trying to sort so the newest notifications are sent first
models.py
class Notifications(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateTimeField('date transacted')
read = models.BooleanField(default=False)
message = models.CharField(max_length=300)
views.py
class getnotifications(viewsets.ModelViewSet):
# Database model
queryset = User.objects.all()
# Serializer - this performs the actions on the queried database entry
serializer_class = NotificationsSerializer
# What field in database model will be used to search
lookup_field = 'username'
serializers.py
class NotificationsSerializer(DynamicModelSerializer):
notifications_set = DynamicRelationField('ListNotificationsSerializer', many=True, embed=True)
class Meta:
model = User
fields = ['notifications_set']
class ListNotificationsSerializer(DynamicModelSerializer):
class Meta:
model=Notifications
name='notifications_set'
fields=['pk','date','read','message']
{
"notifications_set": [
{
"pk": 1,
"date": "2022-10-10T17:11:33.821757Z",
"read": false,
"message": "A user with the phone <omitted> has submitted a restock request for 5 of airpods"
},
{
"pk": 2,
"date": "2022-10-10T00:00:00Z",
"read": false,
"message": "A user with the phone <omitted> has submitted a restock request for 5 of airpods"
},
{
"pk": 3,
"date": "2022-10-10T17:25:11.824385Z",
"read": false,
"message": "A user with the phone <omitted> has submitted a restock request for 5 of airpods"
}
],
"links": {
"notifications_set": "notifications_set/"
}
}
Any help is appreciated, thank you!
The text was updated successfully, but these errors were encountered:
I am trying to sort the results from the django rest framework api, trying to sort so the newest notifications are sent first
models.py
views.py
serializers.py
Accessing the API in postman from this URL: http://localhost:8000/pm/getnotifications//?sort[]=-notifications_set.pk
The JSON results
Any help is appreciated, thank you!
The text was updated successfully, but these errors were encountered: