【问题标题】:Unable to get repr for <class 'django.db.models.query.QuerySet'>无法获得 <class 'django.db.models.query.QuerySet'> 的代表
【发布时间】:2012-11-05 12:24:01
【问题描述】:

调试告诉我:

comments = Comment.get_for_intention(id)

是:

{QuerySet} 无法获取类 'django.db.models.query.QuerySet' 的代表

评论模型:

class Comment(models.Model):
    user = models.ForeignKey(User)
    intention = models.ForeignKey(Intention)
    text = models.TextField()
    created = models.DateTimeField(default=datetime.datetime.now())

    @staticmethod
    def get_for_intention(intention_id):
        return Comment.objects.filter(intention=intention_id)[:10]

我不知道我做错了什么。有人可以给我任何建议吗?

【问题讨论】:

    标签: django django-models django-database


    【解决方案1】:

    我认为问题在于意图。它是外键,你想使用所有意图对象。

    你应该使用

    def get_fot_intention(self):
       return self.Intention.intention_id
    

    如果您只想使用 cmets,则不必定义 get_fot_intention 也不必使用 intent_id ,只需使用 intent 对象即可。例如,您可以使用它:

    def intention_detail(request,slug=None):
    
        intention = get_object_or_404(Intention,slug=slug)
        comments = Comment.objects.filter(intention=intention) 
    

    但你必须有意图地打野。

    【讨论】:

    • 我想获取与意图相关的 cmets。参数id是意图id
    猜你喜欢
    • 2018-10-12
    • 2017-11-06
    • 2018-05-30
    • 1970-01-01
    • 2018-12-03
    • 2016-08-11
    • 2020-12-10
    • 2015-10-13
    • 1970-01-01
    相关资源
    最近更新 更多