6

I have a Table "Configuration".

class Configuration(models.Model):
    inventory_check = models.BooleanField(default=False)
    refund = models.BooleanField(default=False)
    record_seat_number = models.BooleanField(default=False)
    base_url = models.URLField()

This table will have a single entry. Below is the serializer :

class ConfigurationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Configuration
        fields = '__all__'

I am using rest framework for the API. Below is the Views.py

@api_view(['GET'])
def get_configuration(request):
     m = Configuration.objects.all()
     serializer = ConfigurationSerializer(m, many=True)
     return Response(serializer.data, status=status.HTTP_200_OK)

This works perfectly. But the problem is this would return object inside array.

[
{
    "id": 1,
    "inventory_check": false,
    "refund": true,
    "record_seat_number": false,
    "base_url": "http://localhost:8000/"
}
]

All I want is to send only the object without array. How to achieve this?

1
  • 2
    When you do Configuration.objects.all() you get a QuerySet. It converts into array in JSON. If you explicitly need only a single object, you should rethink your design. For example Configuration.objects.get(pk=1) will return a single object, if available. Commented May 10, 2017 at 9:42

2 Answers 2

3
 m = Configuration.objects.get(id=1) # you need to get single object here
 serializer = ConfigurationSerializer(m) # remove many = True
Sign up to request clarification or add additional context in comments.

Comments

2

Simply remove the many=True from the serializer instantiation:

 serializer = ConfigurationSerializer(m)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.