我试图创建单一的循环,将迭代通过每个类别的每个产品的所有大小.我的模特:
class Category(models.Model):
...
class Product(models.Model):
category = models.ForeignKey(Category, db_column="id", on_delete=models.CASCADE, related_name="products")
...
class Size(models.Model):
product = models.ForeignKey(Product, db_column="id", on_delete=models.CASCADE, related_name="sizes")
...
和我的代码在服务中
def adjust_sizes(self, category: Category) -> None:
for size in category.products.sizes.all():
# rest of my function
但当我try 运行此函数时,出现错误:
*** AttributeError: 'RelatedManager' object has no attribute 'sizes'
我想让它单圈运行,有人能帮我吗?