Category对象不是iterab

2024-04-25 19:57:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我是django的初学者 我得到以上错误为“类别不可编辑”

提前谢谢

在模型.py在

这是我的分类模型

class Category(MPTTModel):
    title = models.CharField(max_length =120)
    parent = TreeForeignKey('self' , null = True , blank = True , verbose_name='parent category', related_name='categories')
    description = models.TextField(null=True , blank=True)
    active = models.BooleanField(default=True)
    slug = models.SlugField(unique=True)
    timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)

    def get_absolute_url(self):
            return reverse('categories', kwargs={'path': self.get_path()})

这是我的产品型号

^{pr2}$

给你视图.py 随机导入

class ProductDetailView(DetailView):
    model = Product
    template_name = "products/product.html"
    #template_name = "<appname>/<modelname>_detail.html"
    def get_context_data(self, *args, **kwargs):
        context = super(ProductDetailView, self).get_context_data(*args, **kwargs)
        product = self.get_object()
        #order_by("-title")
        context["related"] = sorted(Product.objects.get_related(product)[:6], key= lambda x: random.random())
        return context

它在这些线路上给了我错误

1-上下文[“相关”]=已排序(Product.objects.get_相关(产品)[:6],键=λx:随机。随机()) 以及

2-相关产品=自我获取查询集().过滤器(类别=产品类别)在


Tags: namepy模型selftruegetmodels错误