为什么`Model.objects.get(id = model.ForeignKeyField).IntegerField`不返回整数?

时间:2018-01-12 12:22:18

标签: python django

我正在尝试根据ForeignKeyField的值从查询集对象中获取IntegerField的值,所以我可以像这样1。

all = Comment.objects.filter(video=Video.objects.get(identifier=request.GET['identifier'])).order_by('-uploaded')[:end]
    for comment in all:
        comment.indent = Comment.objects.get(id=comment.replyto).indent + 1;

然而,它似乎导致错误说:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Comment'

这是为什么?我做错了什么?

评论模型:

class Comment(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, to_field='username')
    video = models.ForeignKey(Video, on_delete=models.CASCADE)
    description = models.TextField(default="none")
    replyto = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE)
    uploaded = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True) # maybe add ability to update comments...
    indent = models.IntegerField(default=0)

完全追溯:

Traceback (most recent call last):
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/trie/Desktop/django/vidmiotest/player/views.py", line 69, in obtain_comments
    comment.indent = Comment.objects.get(id=comment.replyto).indent + 1;
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/query.py", line 394, in get
    clone = self.filter(*args, **kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/query.py", line 836, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/query.py", line 854, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1252, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1276, in _add_q
    split_subq=split_subq,
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1214, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1084, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/lookups.py", line 18, in __init__
    self.rhs = self.get_prep_lookup()
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/lookups.py", line 68, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 947, in get_prep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Comment'

1 个答案:

答案 0 :(得分:2)

replyto可以为空,这意味着你必须检查comment.replyto是否存在:

all = Comment.objects.filter(video=Video.objects.get(identifier=request.GET['identifier'])).order_by('-uploaded')[:end]
    for comment in all:
        if comment.replyto:
            comment.indent = Comment.objects.get(id=comment.replyto.id).indent + 1

但你甚至不需要像你已经拥有它那样发表评论:

all = Comment.objects.filter(video=Video.objects.get(identifier=request.GET['identifier'])).order_by('-uploaded')[:end]
for comment in all:
    if comment.replyto:
        comment.indent = comment.replyto.indent + 1

您可以更进一步,只选择带有回复的评论:

video = Video.objects.get(identifier=request.GET['identifier'])
comments_with_reply = Comment.objects.filter(
    video=video, replyto__isnull=False).order_by('-uploaded')
for comment in comments_with_reply:
    comment.indent = comment.replyto +1