1

私のアプリに機能を実装したいのですが、どうすればいいのかわかりません。私が欲しいのはこれです:私はimagekitを使用して画像を保存するモデルクラスを持っており、ユーザーがそれぞれの車両レコードを編集することなく、車両の画像を簡単に更新できるようにしたいと考えています。

これを行う方法は、という名前のフォルダーがありoriginals、各車両のフォルダーが次の形式で含まれることです<stock_number>/PUBLIC 。ユーザーが車両のフォルダーに画像を移動する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)
4

1 に答える 1

2

私のアドバイスは、crontabジョブでdjangoスクリプトを実行することです。たとえば、5分で5回実行します。

スクリプトは画像フォルダに飛び込み、画像をレコードと比較します。

簡単な例:

# Set up the Django Enviroment
from django.core.management import setup_environ 
import settings 
setup_environ(settings)
import os
from your_project.your_app.models import *
from datetime import datetime

vehicles_root = '/home/vehicles'
for stock_number in os.listdir(vehicles_root):
    cur_path = vehicles_root+'/'+stock_number
    if not os.path.isdir(cur_path):
        continue # skip non dirs
    for file in os.listdir(cur_path):
        if not isfile(cur_path+'/'+file):
            continue # skip non file
        ext = file.split('.')[-1]
        if ext.lower() not in ('png','gif','jpg',):
            continue # skip non image
        last_mod = os.stat(cur_path+'/'+file).st_mtime
        v = Vehicle.objects.get(stock_number=stock_number)
        if v.last_upd < datetime.fromtimestamp(last_mod):
            # do your magic here, move image, etc.
            v.last_upd = datetime.now()
            v.save()
于 2010-09-24T12:02:05.200 に答える