外键引用

2024-04-16 12:50:56 发布

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

我正在尝试获取外键(author)的计数,以便获取用户创建的博客总数。但显示的总数不正确。有人能帮助我们如何获得count的外键引用吗。谢谢。你知道吗

你知道吗型号.py你知道吗

class Blog(models.Model):

     author  = models.ForeignKey(User, on_delete = models.CASCADE, related_name='blogs')
     created_date  =  models.DateTimeField(default=timezone.now)

     def total_likes_received(user):
         return user.blogs.aggregate(total_likes=Count('likes'))

你知道吗视图.py你知道吗

def get_queryset(self):
        return (Blog.objects.filter(date__lte=timezone.now())
                                    .order_by('-date')
                                    .annotate(
                      author_total_likes = Count('author__blogs__likes'),
                      author_total_blogs = Count('author__blogs')
                                             )

Tags: pydatemodelsdefcountblognow外键
2条回答

添加这一行android:imeOptions="actionSend"并在代码中如下处理

 @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }

使用

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);

在XML中:

android:inputType="textMultiLine"

资料来源:Multi-line EditText with Done action button

相关问题 更多 >