English 中文(简体)
Django的简单群体(无计数)
原标题:A simple group-by (no count) in Django
  • 时间:2010-04-23 01:03:56
  •  标签:
  • sql
  • django

如果这是原始的SQL,它就是一个没有人才的人,但在Django,这证明是很难找到的。 我想要的是:

SELECT
  user_id
FROM
  django_comments
WHERE
  content_type_id = ? AND
  object_pk = ?
GROUP BY
  user_id

这最后两条重重重问题。 我要说的是,“Django-way”虽然如此,但我所看到的唯一事情是提及总合和说明,我不认为这些问题已经解决了吗? 如果有人能向我解释这一点,我真心赞赏。

最佳回答

只要能看到你想得到具体评论的作者:GenericObject(say Article)。

Djangos ORM offers following of relationships So the solution would be:

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType

User.objects.filter(comment_comments__content_type=ContentType.objects.get(app_label="articles", model="article"), comment_comments__object_pk="12").distinct()

如果您希望得到用户的帮助,则使用诸如价值观或价值观之类的东西。

User.objects.filter(comment_comments__content_type=ContentType.objects.get(app_label="articles", model="article"), comment_comments__object_pk="12").distinct().values_list( id )

希望会有所助益。

问题回答

这在达詹戈也是一位没有受过训练的人,但你需要停止在“Q”方面的思维,而是问你实际上想要达到什么。

“我希望有一份所有张贴评论的用户身份证清单 此时此刻,你要求:

 Comment.objects.filter(
    content_type_id=foo, object_pk=bar
 ).values_list( user_id , flat=True).distinct()




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签