自动更新图像
我想在我的应用程序中实现一项功能,但我不知道如何去做。我想要的是:我有一个使用 imagekit 保存其图像的模型类,我希望用户能够轻松更新车辆的图像,而无需编辑每个相应的车辆记录。
他们的做法是,会有一个名为 originals
的文件夹,其中包含每辆车的文件夹,格式为
如果用户将图像移动到车辆的 PUBLIC
文件夹中,执行脚本时,会将这些图像与当前图像进行比较,如果 PUBLIC
文件夹中的图像存在,则更新它们较新。如果记录没有图像,则会添加图像。另外,如果图像已从 site_media 目录中删除,则它们的链接也应从数据库中删除。
我怎样才能有效地解决这个问题?我的模型如下:
class Photo(ImageModel):
name = models.CharField(max_length = 100)
original_image = models.ImageField(upload_to = 'photos')
num_views = models.PositiveIntegerField(editable = False, default=0)
position = models.ForeignKey(PhotoPosition)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class IKOptions:
spec_module = 'vehicles.specs'
cache_dir = 'photos'
image_field = 'original_image'
save_count_as = 'num_views'
class Vehicle(models.Model):
objects = VehicleManager()
stock_number = models.CharField(max_length=6, blank=False, unique=True)
vin = models.CharField(max_length=17, blank=False)
....
images = generic.GenericRelation('Photo', blank=True, null=True)
进度更新 我已经尝试了该代码,虽然它有效,但我缺少一些东西,因为我可以获得图像,但在那之后,它们不会传输到 site_media/photos
目录中...我应该这样做还是 imagekit
会自动执行此操作?我有点困惑。
我像这样保存照片:
Photo.objects.create(content_object = vehicle, object_id = vehicle.id,
original_image = file)
I'd like to implement a functionality in an app of mine, but I don't know how to go about it. What I want is this: I have a model class that uses imagekit to save its images, and I'd like to have the users being able to update the images easily for the vehicles without having to edit each respective vehicle record.
How they'll do this is that there will be a folder named originals
and it'll contain folders for each vehicle in the format <stock_number>/PUBLIC
If a user moves images into the PUBLIC
folder for a vehicle, when the script is executed, it'll compare those images with the current ones and update them if those in the PUBLIC
folder are newer. If the record has no images, then they will be added. Also, if the images have been deleted from the site_media directory, then their links should be deleted from the database.
How can I go about this in an efficient way? My models are as below:
class Photo(ImageModel):
name = models.CharField(max_length = 100)
original_image = models.ImageField(upload_to = 'photos')
num_views = models.PositiveIntegerField(editable = False, default=0)
position = models.ForeignKey(PhotoPosition)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class IKOptions:
spec_module = 'vehicles.specs'
cache_dir = 'photos'
image_field = 'original_image'
save_count_as = 'num_views'
class Vehicle(models.Model):
objects = VehicleManager()
stock_number = models.CharField(max_length=6, blank=False, unique=True)
vin = models.CharField(max_length=17, blank=False)
....
images = generic.GenericRelation('Photo', blank=True, null=True)
Progress Update
I've tried out the code, and while it works, I'm missing something as I can get the image, but after that, they aren't transferred into the site_media/photos
directory...am I suppossed to do this or imagekit
will do this automatically? I'm a bit confused.
I'm saving the photos like so:
Photo.objects.create(content_object = vehicle, object_id = vehicle.id,
original_image = file)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是在 crontab 作业中运行 django 脚本,比如说 5 分钟内运行 5 个脚本。
该脚本将深入图像文件夹并将图像与记录进行比较。
一个简化的例子:
My advice is running django script in a crontab job, lets say, 5 in 5 minutes.
The script would dive into the image folders and compare the images with the records.
A simplified example: