Django models 从另一个字段获取django模型字段
我有这个型号Django models 从另一个字段获取django模型字段,django-models,Django Models,我有这个型号 class Person(models.Model): picture = models.ImageField( default='default.jpg', upload_to='profile_pics', ) firstName = models.CharField(blank=True, max_length=100) familyName = models.CharField(blank=True, max_length=100
class Person(models.Model):
picture = models.ImageField(
default='default.jpg', upload_to='profile_pics', )
firstName = models.CharField(blank=True, max_length=100)
familyName = models.CharField(blank=True, max_length=100)
age = models.IntegerField(default=0)
GENDER = [
("M", 'Male'),
("F", 'Female'),
("U", 'UNKNOWN'),
]
gender = models.CharField(
max_length=2,
choices=GENDER,
default="U",
)
address = models.CharField(blank=True, max_length=100)
remark = models.TextField(default="no remark")
description_vector = models.TextField(blank=True)
每当我添加新人物或更改模型图像时,我想从图片字段推断描述向量(使用名为identifie(picture)的方法返回字符串)(如果图像没有更改,我不想更改描述向量)
我知道我可以使用像这样的保存方法,但我不知道如何指定图像更改时向量的更改
我不知道它是否改变了什么,但我使用了django rest framowrk来添加和更改人员
我知道我不确定我是否理解您的具体疑问,但我认为这可能会有所帮助
def save(self, *args, **kwargs):
if self.id is not None: # check only when update
original_picture = self.objects.get(id=self.id).picture
if original_picture !== self.picture # You must add here your method to evaluate if both images are equal
self.vector = some_method_to_change_vector(self.picture)
return super().save(*args, **kwargs)
是的,我想这就是我需要的我会试试。。。。我想问的另一件事是,如果你熟悉这一点,如何从图片中获取图像数据。。。我的意思是如何获取原始图像数据,就像我用open('imagePath','rb')读取一样。read()@mahmoudettouahri不太可能。我建议你为此再发一个问题。