对Django ImageFileField进行子类化 [英] Subclassing the Django ImageFileField

查看:89
本文介绍了对Django ImageFileField进行子类化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣将django的ImageFileField子类化以允许访问图像IPTC元数据,例如:

I'm interested in subclassing django's ImageFileField to allow access to the image IPTC metadata, something like:

>>> from myapp.models import SomeModel
>>> obj = SomeModel.objects.all()[0] # or what have you
>>> obj.image.iptc['keywords']
('keyword','anotherkeyword','etc')

... 文档说 阅读django的内部代码,我做了我试图制作一个工作实现,我不知道我在做什么 - 以前我已经定义了自定义字段,但是我无法提出基于文件的字段的样板设置。

... the docs say to read over django's internal code, which I did; I've tried to produce a working implementation and I am not sure what I'm doing -- I've defined custom fields before, but I can't come up with boilerplate setup for a file-based field.

我知道我需要定义一个 attr_class 和一个 descriptor_class 这行得通。有人有一个简单的例子或建议,我可以开始吗?

I know I need to define an attr_class and a descriptor_class to make it work. Does anyone have a straightforward example or suggestion, with which I could get started?

推荐答案

你的问题不清楚:有没有尝试这样的东西?

It's not clear from your question: have you tried something like this?

class ImageMetadataMixin(object):
    """Mixin can be added to any image file"""
    @property
    def iptc(self):
        """Or something like this"""

class ImageWithMetadataFieldFile(ImageMetadataMixin, ImageFieldFile):
    pass

class ImageWithMetadataField(ImageField):
    attr_class = ImageWithMetadataFieldFile


$ b $我认为这是必要的。为什么你认为你需要重新定义 descriptor_class

这篇关于对Django ImageFileField进行子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆