每当我从管理面板中创建一个新条目并将其通过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