2017-05-23 12 views
0

を文字列のリストをシリアル化するために、次のように私は、Djangoの残りの枠組みの中でのシリアライザを持っている:どのようにDjangoの残りのフレームワークで

class StatePictureSerializer(serializers.ModelSerializer): 
    blob_url = Field(source='public_url') 

    class Meta: 
     model = Inspection_Picture 
     fields = ('blob_url',) 

を次のように私は何かを得る結果として、次のように

class StateSerializer(serializers.ModelSerializer):  

    kilometers = Field(source='mileage') 
    pictures = StatePictureSerializer(many=True, read_only=True) 

    class Meta: 
     model = Inspection # Options 
     fields = ('kilometers', 'inspection_date', 'pictures') 

そしてStatePictureSerializerがあります:

{  
    "kilometers": 64431, 
    "inspection_date": null, 
    "pictures": [ 
     {"blob_url": "path/to/photo"}, 
     {"blob_url": "path/to/photo"}, 
     {"blob_url": "path/to/photo"}, 
     {"blob_url": "path/to/photo"}, 
     {"blob_url": "path/to/photo"} 
    ] 
} 

したがって、picturesはオブジェクトの配列です。私が欲しいもの

は、例えば、文字列の配列、次のとおりです。

"pictures": ["path/to/photo", "path/to/photo", "path/to/photo", "path/to/photo", "path/to/photo"] 

それを行うにはどのように任意のアイデア?次のように

EDIT

Inspectionモデルは次のように

class Inspection(models.Model): 
    customerReference = models.CharField(max_length=50, blank=True, null=True) 
    extraReference = models.CharField(max_length=50, blank=True, null=True) 
    itemReference = models.IntegerField(blank=True, null=True) 
    vehicle = models.ForeignKey(to=Vehicle) 
    mileage = models.IntegerField() 
    timeStamp = models.DateTimeField(auto_now_add=True) 
    inspection_date = models.DateTimeField(null=True) 
    features = models.ManyToManyField(to=Feature) 
    pictures = models.ManyToManyField(to=Images, through="Inspection_Picture") 
    damages = models.ManyToManyField(to=Damage) 
    parts = models.ManyToManyField(to=Part) 
    checks = models.ManyToManyField(to=CheckType, through=Inspection_Check) 
    featuresFlat = models.ManyToManyField(to=FeatureFlat, through=Inspection_FeatureFlat) 

そしてImagesモデルは次のとおりです。

class Images(models.Model): 
    """Model for storing uploaded photos""" 
    filename = models.CharField(max_length=255) 
    extension = models.CharField(max_length=40) 
    key_data = models.CharField(max_length=90, unique=True, blank=True, null=True) 
    upload_date = models.DateTimeField(auto_now_add=True) 
    upload_identification = models.CharField(max_length=50, blank=True, null=True) 
    url = models.CharField(max_length=1024, blank=True, null=True) 
    stored = models.BooleanField(default=False) 
    thumbnailed = models.BooleanField(default=False) 
    thumbnailed_treated = models.BooleanField(default=False) 
    protected = models.BooleanField(default=False) 
    source = models.CharField(max_length=50, blank=True, null=True) 

    @property 
    def key_generate(self): 
     """returns a string based unique key with length 80 chars""" 
     while 1: 
      key = str(random.getrandbits(256)) 
      try: 
       Images.objects.get(key=key) 
      except: 
       return key 

    def __unicode__(self): 
     return self.upload_identification 

    def public_url(self): 
     return settings.AZURE_URL_FULL + self.url 

答えて

3

私はあなたのケースでSerializerMethodFieldはとして正しい選択だと思います続く。下のコードに<field_name>の不一致があります。あなたのモデルに従って動作させるようにしてください。上記のシリアライザに基づいてフィールド名を仮定します。

class StateSerializer(serializers.ModelSerializer):  

    kilometers = Field(source='mileage') 
    pictures = serializers.SerializerMethodField('get_pictures') 

    class Meta: 
     model = Inspection # Options 
     fields = ('kilometers', 'inspection_date', 'pictures') 

    def get_pictures(self, obj): 
     return [each.public_url() for each in obj.pictures.all() ] 
+0

「ManyRelatedManager」オブジェクトが「iterable」エラーではありません。私はこれら2つのモデルで質問を更新しました。 – Boky

+1

メソッドを次のように変更してください。 '' 'obj.pictures.all()]' ''のそれぞれに対してreturn [each.public_url()を実行してください。それは問題を解決するはずです。 –

+0

それはトリックでした。 – Boky

関連する問題