有没有一种方法可以将函数的返回值用作Django归档文件中的默认值?

时间:2020-07-06 17:08:37

标签: python django django-models model

每当我从管理面板中创建一个新条目并将其通过getimg()函数传递并将返回值存储在自动保存在Forimg表中的图像中时,我都想获取Product_desciption的值。

class Products(models.Model):

    Code = models.CharField(max_length=10)
    Product_description = models.CharField(max_length=200,primary_key = True)
    Val_tech = models.CharField(max_length=5)
    Quantity = models.IntegerField(default=0)
    UOM = models.CharField(max_length=5)
    Rate = models.FloatField(default=0)
    Value = models.FloatField(default=0)


    def __str__(self):
        return self.Product_description


class Forimg(models.Model):

    @staticmethod
    def getimg(item):
        page = requests.get(f'https://www.google.com/search?tbm=isch&q={item}')
        soup = BeautifulSoup(page.text, 'lxml')
        item_head = soup.find('img',class_='t0fcAb')
        return item_head['src']

    products = models.OneToOneField(Products, on_delete=models.CASCADE, primary_key = True)
    image = models.ImageField(upload_to = 'img/', width_field= 30, height_field = 40)


    def __str__(self):
        return self.products.Product_description

0 个答案:

没有答案