Django(2.0.7)Wagtail(2.1.1)序列化图像字段

时间:2018-08-08 17:33:04

标签: django api django-forms django-rest-framework wagtail

我有一个片段

@register_snippet
class TripStopItem(models.Model, index.Indexed):

    DISPLAYED = 'DSP'
    NOTDISPLAYED = 'NDSP'
    UNKNOWN = 'UNK'

    DISPLAY_STATUS_CHOICES = (
        (DISPLAYED, 'DISPLAYED'),
        (NOTDISPLAYED, 'NOTDISPLAYED'),
        (UNKNOWN, 'Unknow'),
    )

    status = models.CharField(
        max_length=4,
        choices=DISPLAY_STATUS_CHOICES,
        default=UNKNOWN,
    )
    entered_at = models.DateTimeField(default=datetime.datetime.now())
    image =  models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

from rest_framework import serializers as rest_serializers

class TripStopItemSerializer(rest_serializers.ModelSerializer):
    image_url = rest_serializers.SerializerMethodField()
    _image = rest_serializers.Field('image.url')
    class Meta:
        model = TripStopItem
        fields = ('id','status','tripstop','entered_at','image','campaign','notes')

我想知道如何检索包括图像的整个对象。通过获取url或base64选项。我使用的不是API,因为它似乎总是只返回ID号,所以我使用的基本上是表单端点。

def getDataEndpoint(request):

    _id = request.POST.get('id').strip()

    if (_rider is not None):
        try:
            b_qs = TripStopItemSerializer.objects.get(id=_id)
            pprint(b_qs)
            if (results):              
                data = {'response': 200,'data':data}
            else:
                data = {'response': 400,'message':'No Open rides for Rider'}


        except Exception as e:
            data = {'response': 401,'message':e}
    else:
        data = {'response': 402,'message':'Invalid username or password. Kindly try again.'}

    return JsonResponse(data)

0 个答案:

没有答案