我有两个模型如下
class Product(models.Model):
product_name = models.CharField(max_length=100)
product_weight = models.CharField(max_length=30)
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete=models.DO_NOTHING)
image = models.ImageField(upload_to='/images/{product_id}/', blank=True)
如何在ProductImage模型中提取产品标识
提前谢谢
以下是我到目前为止所做的尝试,并找到了解决方案。我发现实现的唯一选择是使用pre_save和post_save信号。下面是我如何实现解决方案的。如果有人有不同的解决方案,请分享。谢谢
通过在字段名中添加“\u id”,可以获得Django中任何外键的“原始”值
您也可以只跟踪关系,但是如果没有使用类似
select_related
的方法缓存关系,那么这将执行另一个数据库查找相关问题 更多 >
编程相关推荐