0

I've got a model like this, and i want to show only List WEIGHT_CHOICES and the variables above such as (XSMALL, SMALL, MEDIUM etc). But when i have no idea what queryset should i make. Because when i put MyAnimal.objects.all() it shows me what i want, but it shows me it (if there are 100 instances in db) 100 times. Do you know how can i either get only one instance or just those choices as json file?

class MyAnimal(models.Model):

    XSMALL = 'XS'
    SMALL = 'S'
    MEDIUM = 'M'
    LARGE = 'L'
    XLARGE = 'XL'
    XXLARGE = 'XXL'

    WEIGHT_CHOICES=[
    (XSMALL, '>1kg'),
    (SMALL, '1-5kg'),
    (MEDIUM, '5-10kg'),
    (LARGE, '10-25kg'),
    (XLARGE, '25-50kg'),
    (XXLARGE, '<50kg'),]

    weight = models.CharField(max_length=3, choices=WEIGHT_CHOICES, default=MEDIUM,)

class AnimalWeightSerializer(serializers):
    class Meta:
        model = MyAnimal
        fields = ('WEIGHT_CHOICES', 'XSMALL', 'SMALL', 'MEDIUM', 'LARGE', 'XLARGE', 'XXLARGE',)
class AnimalWeightList(generics.ListAPIView):
    queryset = MyAnimal.objects.all()
    serializer_class = AnimalWeightSerializer

1 Answers1

0

As described here every models has a _meta class and you can access it to get your field choices

choices_models = MyAnimal._meta.get_field('weight').choices
Luiz
  • 1,985
  • 6
  • 16