匹配查询不存在?

时间:2019-05-23 11:50:02

标签: django django-models django-rest-framework django-views django-rest-auth

我正在执行转发功能,并且运行起来很流畅,但是我无法转发自己的推文,我可以转发其他用户的推文,但不能转发我的推文 。它表明不存在匹配的查询。

这是推文模型

class TweetManager(models.Manager):
   def retweet(self,user,parent_obj):
    if parent_obj.parent:
        obj_parent = parent_obj.parent
    else:
        obj_parent = parent_obj

    qs = self.get_queryset().filter(user = user, parent = obj_parent)
    if qs.exists():
        return None

    obj = self.model(
        user = user,
        parent = obj_parent,
        content = parent_obj.content

        )

    obj.save()

    return obj      



class Tweet(models.Model):
   parent = models.ForeignKey("self",blank = True,null = True)
   user = models.ForeignKey(settings.AUTH_USER_MODEL)
   content = models.CharField(max_length = 130)
   time = models.DateTimeField(auto_now_add = True)
   objects = TweetManager()

   def __str__(self):
       return self.content

   class Meta:
       ordering = ['content']   

这是views.py

 class Retweet(View):
    def get(self, request, pk, *args, **kwargs):
      tweet = get_object_or_404(Tweet, pk=pk)
      if request.user.is_authenticated:
        new_tweet = Tweet.objects.retweet(request.user, tweet)
        return HttpResponseRedirect("/")
    return HttpResponseRedirect(tweet.get_absolute_url())

0 个答案:

没有答案