如何存储图像,例如Images API

问题描述 投票:1回答:1

[嗨,我的用于创建简单成员的模型已经搜索了多个位置,到目前为止,我还没有弄清楚如何将图像的目录存储在我的[[image字段旁边。

class Member(models.Model): with open(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'/api/data/states.json') as s: states_json = json.load(s) STATES = [(str(state["nome"]), str(state["sigla"])) for state in states_json] name = models.CharField(max_length=100, null=True, blank=True, verbose_name='Nome') image = models.ForeignKey( "wagtailimages.Image", blank=False, null=True, related_name="+", on_delete=models.SET_NULL, verbose_name='Imagem do membro', ) role = models.CharField(max_length=35, null=True, blank=True, verbose_name='Cargo') city = models.CharField(max_length=30, null=True, blank=True, verbose_name='Cidade') state = models.CharField( verbose_name='Estado', max_length=19, choices=STATES, default=('SP'), ) created = models.DateField(default=timezone.now, verbose_name='Atualizado') modified = models.DateField(default=timezone.now, verbose_name='Modificado') panels = [ FieldPanel("name"), ImageChooserPanel("image"), FieldPanel("role"), FieldPanel("city"), FieldPanel("state"), FieldPanel("created"), FieldPanel("modified"), ] def __str__(self): return self.name class Meta: verbose_name = 'Membro' verbose_name_plural = 'Membros'
这正常工作,但是如何存储Wagtail Images API这样的图像信息?

示例:

"detail_url": "http://localhost/api/v2/images/2/", "download_url": "/media/original_images/l8GlI3V.jpg"

这是我的

JSON

,来自API[ { "id": 6, "name": "Gabriel", "role": "Desenvolvedor", "image": 4, "city": "Itapetininga", "state": "São Paulo", "created": "2020-04-26", "modified": "2020-04-26" } ]

感谢您的帮助!

python django django-models django-rest-framework wagtail
1个回答
0
投票
This是文档中的链接,Wagtail具有DRF的依赖项,创建所需的序列化器并将图像表示为链接而不是pk,这应该很简单(请参阅DRF文档)。

This自定义序列化程序上的视频也应该有所帮助。

热门问题
推荐问题
最新问题