django连接多个表的查询集

发布于 2024-09-05 12:51:16 字数 442 浏览 3 评论 0原文

如果我对多个表有查询,例如:

d = Relations.objects.filter(follow = request.user).filter(date_follow__lt = last_checked)
r = Reply.objects.filter(reply_to = request.user).filter(date_reply__lt = last_checked)
article = New.objects.filter(created_by = request.user)
vote = Vote.objects.filter(voted = article).filter(date__lt = last_checked)

并且我想显示按日期排序的所有表的结果(我的意思是不列出所有回复,然后列出所有投票等)。 不知何故,我想在一个查询集中“加入所有这些结果”。 有可能吗?

If I have queries on multiple tables like:

d = Relations.objects.filter(follow = request.user).filter(date_follow__lt = last_checked)
r = Reply.objects.filter(reply_to = request.user).filter(date_reply__lt = last_checked)
article = New.objects.filter(created_by = request.user)
vote = Vote.objects.filter(voted = article).filter(date__lt = last_checked)

and I want to display the results from all of them ordered by date (I mean not listing all the replies, then all the votes, etc ).
Somehow, I want to 'join all these results', in a single queryset.
Is there possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-09-12 12:51:16

看起来你需要不同的对象来进行共同的操作......

1)在这种情况下,最好将这些属性抽象到一个超类中......我的意思是你可以有一个 Event 类它定义了一个 user 字段,并且所有其他事件类都会对其进行子类化。

class Event(model.Model):
    user = models.ForeignKey(User)
    date = ...

class Reply(Event):
    #additional fields

class Vote(Event):
    #additional fields

然后您就可以执行以下

Event.objects.order_by("date") #returns both Reply, Vote and Event

检查 http:// /docs.djangoproject.com/en/1.2/topics/db/models/#id5 有关模型继承的信息。

2) 您还可以拥有一个与另一个对象具有通用关系的 Event 模型。这对我来说听起来更清晰,因为投票在概念上不是“事件”。签出: http://docs.djangoproject.com/en /dev/ref/contrib/contenttypes/#id1

无论如何,我认为你的问题是设计问题

It seems like you need different objects to have common operations ...

1) In this case it might be better to abstract these properties in a super class... I mean that you could have an Event class that defines a user field, and all your other event classes would subclass this.

class Event(model.Model):
    user = models.ForeignKey(User)
    date = ...

class Reply(Event):
    #additional fields

class Vote(Event):
    #additional fields

Then you would be able to do the following

Event.objects.order_by("date") #returns both Reply, Vote and Event

Check-out http://docs.djangoproject.com/en/1.2/topics/db/models/#id5 for info on model inheritance.

2) You could also have an Event model with a generic relation to another object. This sounds cleaner to me as a Vote is conceptually not an "event". Check-out : http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1

Anyway, I think your problem is a matter of design

荒芜了季节 2024-09-12 12:51:16

除了 Sebastien 的建议 2 之外:Django 实际上有一些内置功能,您可以为此“滥用”;对于管理员来说,它已经有一个模型,可以记录用户的操作并通过通用外键关系引用对象,我认为您可以对该模型进行子类化并将其用于您的目的:

from django.contrib.admin.models import LogEntry, ADDITION
from django.utils.encoding import force_unicode
from django.contrib.contenttypes.models import ContentType

class MyLog(LogEntry):
    class Meta(LogEntry.Meta):
        db_table_name = 'my_log_table' #use another name here 

def log_addition(request, object):
    LogEntry.objects.log_action(
        user_id         = request.user.pk,
        content_type_id = ContentType.objects.get_for_model(object).pk,
        object_id       = object.pk,
        object_repr     = force_unicode(object),
        action_flag     = ADDITION
    )

您现在可以记录所有通知等。它们与 log_addition(request, object) 一起发生并过滤日志表而不是为了您的目的!如果您还想记录更改/删除等,您可以为此创建一些辅助函数!

In addition to to Sebastien's proposal number 2: Django actually has some built-in functionality that you could "abuse" for this; for the admin it has already a model that logs the user's actions and references the objects through a generic foreign key relation, I think you could just sub-class this model and use it for your purposes:

from django.contrib.admin.models import LogEntry, ADDITION
from django.utils.encoding import force_unicode
from django.contrib.contenttypes.models import ContentType

class MyLog(LogEntry):
    class Meta(LogEntry.Meta):
        db_table_name = 'my_log_table' #use another name here 

def log_addition(request, object):
    LogEntry.objects.log_action(
        user_id         = request.user.pk,
        content_type_id = ContentType.objects.get_for_model(object).pk,
        object_id       = object.pk,
        object_repr     = force_unicode(object),
        action_flag     = ADDITION
    )

You can now log all your notifications etc. where they happen with with log_addition(request, object) and filter the Log table than for your purposes! If you want to log also changes / deletions etc. you can make yourself some helper functions for that!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文