0

私は次のモデルを持っています:

Class A(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    thumbnail = models.ImageField(...)

class B(models.Model)
   title = models.CharField()

   def save(*args, **kwargs):
     # Based on the title field I want to fetch some picture and then save the thumbnail in A

A から参照する必要がある B のようなクラスが他にもあります (これが GenericForeignKey を使用する理由です)。私が理解しようとしている問題はsave()、B のメソッドにいるときに (A に) サムネイル フィールドを保存する方法です。A に多くの if ステートメントを挿入して、参照されているクラスの型を確認し、それに応じてサムネイルを保存するのはかなり面倒です。 .

4

1 に答える 1