动态地将字段添加到模型(或将一些字段存储在单独的类中)

时间:2021-11-01 21:20:43

I want to use ImageSpecField of django-imagekit for resizing image, so I don't want to create a new ImageSpecField field for every ImageField in my models, I want to create a new class or modelField which contains original image and image thumbnail and instantiate this new modelField in my models for images.

我想使用django-imagekit的ImageSpecField来调整图像大小,所以我不想为我的模型中的每个ImageField创建一个新的ImageSpecField字段,我想创建一个包含原始图像和图像缩略图的新类或modelField并实例化这个新模型在我的模型中用于图像。

for example if my model has profile_picture field, I want to have both profile_picture and profile_picture_thumbnail. If my model has avatar field I want to have both avatar and avatar_thumbnail

例如,如果我的模型有profile_picture字段,我想同时拥有profile_picture和profile_picture_thumbnail。如果我的模特有头像字段,我想同时拥有头像和头像

So is there any way to dynamically generate these fields for models?

那么有没有办法为模型动态生成这些字段?

What is the best approach to do that?

这样做的最佳方法是什么?

1 个解决方案

#1


0  

As far as I know, Dynamically injecting fields to a django model is not an easy task. If you want to try , this can be a starting point for you.

据我所知,动态注入django模型的字段并不是一件容易的事。如果您想尝试,这可以作为您的起点。

Similar post can be found here. http://www.sixpearls.com/blog/2013/feb/django-class_prepared-signal/

类似的帖子可以在这里找到。 http://www.sixpearls.com/blog/2013/feb/django-class_prepared-signal/

from django.db.models.signals import class_prepared
def add_image_fields(sender, **kwargs):
    """
    class_prepared signal handler that checks for the model massmedia.Image
    and adds sized image fields
    """
    if sender.__name__ == "Image" and sender._meta.app_label == 'massmedia':
        large = models.ImageField(upload_to=".", blank=True, verbose_name=_('large image file'))
        medium = models.ImageField(upload_to=".", blank=True, verbose_name=_('medium image file'))
        small = models.ImageField(upload_to=".", blank=True, verbose_name=_('small image file'))

        large.contribute_to_class(sender, "large")
        medium.contribute_to_class(sender, "medium")
        small.contribute_to_class(sender, "small")

class_prepared.connect(add_image_fields)

which basically attach a function to create those fields.

它基本上附加了一个函数来创建那些字段。

You can catch class_prepared signal and list all fields, if its a image field, then contribute to class image_field_thumbnail

您可以捕获class_prepared信号并列出所有字段(如果是图像字段),然后将其提供给类image_field_thumbnail

To get all the fields belongs to a model , you can use get_fields

要使所有字段都属于模型,可以使用get_fields

#1


0  

As far as I know, Dynamically injecting fields to a django model is not an easy task. If you want to try , this can be a starting point for you.

据我所知,动态注入django模型的字段并不是一件容易的事。如果您想尝试,这可以作为您的起点。

Similar post can be found here. http://www.sixpearls.com/blog/2013/feb/django-class_prepared-signal/

类似的帖子可以在这里找到。 http://www.sixpearls.com/blog/2013/feb/django-class_prepared-signal/

from django.db.models.signals import class_prepared
def add_image_fields(sender, **kwargs):
    """
    class_prepared signal handler that checks for the model massmedia.Image
    and adds sized image fields
    """
    if sender.__name__ == "Image" and sender._meta.app_label == 'massmedia':
        large = models.ImageField(upload_to=".", blank=True, verbose_name=_('large image file'))
        medium = models.ImageField(upload_to=".", blank=True, verbose_name=_('medium image file'))
        small = models.ImageField(upload_to=".", blank=True, verbose_name=_('small image file'))

        large.contribute_to_class(sender, "large")
        medium.contribute_to_class(sender, "medium")
        small.contribute_to_class(sender, "small")

class_prepared.connect(add_image_fields)

which basically attach a function to create those fields.

它基本上附加了一个函数来创建那些字段。

You can catch class_prepared signal and list all fields, if its a image field, then contribute to class image_field_thumbnail

您可以捕获class_prepared信号并列出所有字段(如果是图像字段),然后将其提供给类image_field_thumbnail

To get all the fields belongs to a model , you can use get_fields

要使所有字段都属于模型,可以使用get_fields