【问题标题】:Field in Model Not Recognized For Migrations模型中的字段无法识别迁移
【发布时间】:2014-10-11 18:24:16
【问题描述】:

在我的 UserProfile 模型中,我只是在此处添加了 django-imagekit 和 thumbnail 字段来为我创建缩略图,但是当我运行 ./manage.py makemigrations 时,django 说没有检测到任何更改,并且 thumbnail 字段没有添加到数据库中。

这是我的代码。我正在使用 Python 3.4 和 Django 1.7:

from django.db import models
from django.contrib.auth.models import User

from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill
from phonenumber_field.modelfields import PhoneNumberField

def get_upload_file_name(instance, filename):
        return '/'.join([instance.user_auth.email, filename])

# Create your models here.
class UserProfile(models.Model):
    user_auth = models.OneToOneField(User, related_name="profile")
    phone = PhoneNumberField(null=True, blank=True, verbose_name="Phone number")
    birth_date = models.DateField(verbose_name="Date of Birth", null=True, blank=True)
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
        ('N', 'Not Specified'),
    )
    gender = models.CharField(
        max_length=1, choices=GENDER_CHOICES, blank=False, default='N', verbose_name='Gender')
    pic = models.ImageField(upload_to=get_upload_file_name,
                            width_field="width_field",
                            height_field="height_field",
                            null=True,
                            blank=True,
                            verbose_name="Profile Picture"
                           )
    height_field = models.PositiveIntegerField(null=True, default=0)
    width_field = models.PositiveIntegerField(null=True, default=0)
    thumbnail = ImageSpecField(source='pic',
                                   processors=[ResizeToFill(120,120)],
                                   format='JPEG',
                                   options={'quality': 60})

【问题讨论】:

    标签: django django-models django-imagekit


    【解决方案1】:

    嗯,从技术上讲,它不是写入数据库的字段。因此,您不必为正在使用的任何东西(SQLite、MySQL、PostgreSQL 等)进行任何迁移。当您访问您的UserProfile.thumbnail 时,它将根据图片 ImageField 中的数据为您创建图像。

    来自documentation

    另一方面,ImageSpecFields 是虚拟的——它们不向其中添加任何字段 您的数据库,不需要数据库。这对很多人来说很方便 原因,但这意味着图像文件的路径需要是 基于源图像和规范以编程方式构建。

    如果您希望它生成缩略图并保存,那么您应该使用ProcessedImageField

    【讨论】:

    • 谢谢。我想我应该更仔细地阅读文档。
    猜你喜欢
    • 2020-05-07
    • 2021-12-14
    • 2018-06-27
    • 1970-01-01
    • 2015-08-03
    • 2016-11-05
    • 2014-06-14
    • 1970-01-01
    • 2020-06-03
    相关资源
    最近更新 更多