1👍
Try this (Considering your comment as well):
class TeamSerializer(serializers.ModelSerializer):
ticker = serializers.PrimaryKeyRelatedField(queryset=Team.objects.all().order_by('priority')[:5])
class Meta:
model = Team
fields = ['name', 'resignation', 'org_id']
1👍
Try this using this custom logic.
def get_limited_teams(self, request, pk=None):
teams= Team.objects.all().order_by('priority')[:5] # Filter,order, no of rows(5) based on requirement
serializer = TeamsSerializer(teams, many=True)
return Reponse(serializer.data)
0👍
You can patch the attribute, with:
@api_view(['GET'])
def org_api(request, org_id):
if request.method == 'GET':
try:
org = Organization.objects.get(org_id=org_id)
org.top_teams = org.teams.order_by('name')[:5] # order by some field
except Organization.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializers = OrgSerializers(org)
return Response(serializers.data)
and in the serializer, serialize top_teams
instead:
class OrgSerializers(serializers.ModelSerializer):
top_teams = TeamSerializer(many=True, read_only=True)
class Meta:
model = Organization
fields = ['org_id', 'name', 'top_teams']
- [Django]-Django tables 2 – how do i change the queryset of the model i am displaying?
- [Django]-Django – Can't pass Environment Variable to Apache/Passenger on the WSGI Interface
Source:stackexchange.com