0

I have a model like this:

## model:
class Product(models.Model):
    id = models.PositiveIntegerField(unique = True)
    price = models.ForeignKey(Price, on_delete = models.CASCADE, null = True, blank = True)

## serializer:
class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ["id", "price",]

which shows up in the template of the API like this:

enter image description here

I get, this is because of the nature of the model field type. Two questions here:

  1. Can I get rid of the little arrows somehow and make a normal field? enter image description here
  2. Could I somehow return a list of all Product ids like for the price? Something like ids = Product.objects.values_list("id", flat = True) or so? enter image description here
xtlc
  • 1,070
  • 1
  • 15
  • 41
  • For your first question, the best solution would be to customize the admin view, like this: https://docs.djangoproject.com/en/3.1/ref/contrib/admin/ – Ryan Mar 25 '21 at 21:45
  • 1
    Do you want only to change the visual field on the API Browser, or to change the behavior of the field all along? You can specify the field explicitly in the serializer to change it's type to a CharField or a IntegerField with choices, and then build the choices dynamically with a QuerySet. – Dan Yishai Mar 25 '21 at 21:51
  • I think my second question is exactly what you said with changing the type to `CharField` (or choices). Can you show me how? – xtlc Mar 25 '21 at 21:53
  • @Ryan how does the adminview change my form actions of the restframework template? – xtlc Mar 26 '21 at 07:48

0 Answers0