T
T
tekleworm2018-04-16 13:36:06
Django
tekleworm, 2018-04-16 13:36:06

Send image url?

There is a group model

class Group(models.Model):
 
    group_name = models.CharField(max_length=100,, unique=True)                    # group's name
    group_description = models.CharField(max_length=500, verbose_name='Опис')                           # short description of the group
    group_image = models.ImageField(upload_to='base/static/images/', ') 
    images_for_slider = models.ManyToManyField('ImagesForSlider', related_name='group')

and picture model
class ImagesForSlider(models.Model):
    image = models.ImageField(upload_to='base/static/images/')
    image_name = models.CharField(max_length=100, verbose_name='Назва', unique=True, default="Image")

    def __str__(self):
        return self.image_name

    def get_img(self):
        return self.image.url

In the serializer class in the meta, all the fields are there. But when I look at what the server gives, then
group_name	"!!!!!!!!!"
group_description	"@@@@@@@@@@"
group_image	"/base/static/base/static/images/git_aCw8bze.png"

in the group_image field everything is fine. and the array images_for_slider is like this
images_for_slider	
0	1
1	2
2	3
3	4

although instead of 1, 2, 3, 4 there should also be links. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2018-04-16
@tekleworm

If I understand correctly, it is necessary to display links, not just IDs. For the manytomany field model, you need to make a separate serializer, something like this:

class ImagesForSliderSerializer(serializers.ModelSerializer):

    class Meta:
        model = ImagesForSlider
        fields = ('id', 'image', 'image_name')

class GroupSerializer(serializers.ModelSerializer):
    images_for_slider = ImagesForSliderSerializer(many=True)
    
    class Meta:
        model = Group
        fields = (
            # ...
            'images_for_slider',
        )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question