0

enter image description here

I want to get the string equivalent of the field named frontal in the database and see it. frontal a manytomanyfield.

I can access the foreground in my model with "Advertising.frontal.get". But how can I see this in restapi.

The value of the frontal area in restapi appears numerical. Because of the way I describe this area. This is normal. But I know the method I should use when viewing (this method: Advertise.frontal.get). How do I view it in restapi with this method.

     ## Serializer.py ##
            
    class AdvertiseMainSerializer(ModelSerializer):
        class Meta:
            model = Advertise
            fields = '__all__'



     ## views.py##

    class advertise(ListAPIView):
        serializer_class = AdvertiseMainSerializer
        queryset = Advertise.objects.all()

   ## model.py##



    class Advertise(models.Model):
        owner = models.ForeignKey(to="user.User", on_delete=models.CASCADE, null=True)
        title = models.CharField(max_length=100, verbose_name="ilan başlığı")
        frontal = models.ManyToManyField(to="advertise.Frontal")
   
sabcan
  • 407
  • 3
  • 15
  • Does this answer your question? [Django rest framework serializing many to many field](https://stackoverflow.com/questions/33182092/django-rest-framework-serializing-many-to-many-field) – Iain Shelvington Aug 18 '20 at 18:22
  • unfortunately this does not solve it because i already want to operate on the field of a single model. very thank you. – sabcan Aug 18 '20 at 18:28

1 Answers1

0

THAT'S İT...

I solved the problem. You can view the string value of your Manymanyfield field using "StringRelatedField" in restframework.

usage method:
yourfieldname = StringRelatedField(many=True)

source: https://www.django-rest-framework.org/api-guide/relations/

thank you for everything

sabcan
  • 407
  • 3
  • 15