显示数据按日期排序,当数据来自Django中的多个模型时

huangapple go评论74阅读模式
英文:

Display data by date, when data comes from multiple models in Django

问题

我目前正在使用Django 4.1开发一个应用程序。我试图显示来自数据库的一些数据的简历。以下是我的模型的简化示例:

class Signal(models.Model):
   data1 = models.CharField()
   data2 = models.CharField()
   date = models.DateTimeField()

class Ban(models.Model):
   data3 = models.CharField()
   data4 = models.CharField()
   date = models.DateTimeField()

我尝试做的是从这两个模型中获取经过筛选的数据,并将所有这些数据按日期排序,以便显示,类似于:

  • Ban1 (2023年3月8日)
  • Ban2 (2023年3月7日)
  • Signal2 (2023年3月6日)
  • Ban3 (2023年3月5日)
  • Signal1 (2023年3月4日)

感谢您提前提供的想法。

英文:

I'm currently working on an app with Django 4.1. I try to display a resume of some data comming from the database. Here is a simplified example of my models:

class Signal(models.Model):
   data1 = models.CharField()
   data2 = models.CharField()
   date = models.DateTimeField()


class Ban(models.Model):
   data3 = models.CharField()
   data4 = models.CharField()
   date = models.DateTimeField()

What I'm trying to do is getting filtered data from these 2 models, and make a list ordered by date from all this data to display it. someting like:

  • Ban1 (08/03/2023)
  • Ban2 (07/03/2023)
  • Signal2 (06/03/2023)
  • Ban3 (05/03/2023)
  • Signal1 (04/03/2023)

Thanks in advance for your ideas

答案1

得分: 1

可以使用.values()来去掉模型层并生成包含完全相同字段的字典。

然后,您可以使用.union()来将数据收集到一个单一的查询集中。

然后,一个简单的.order_by()应该能够完成这个任务。

如果data1data3应该在同一列中,可以这样做:

signals = signals.annotate(new_data1=F("data1"), new_data2=F("data2")).values("new_data1", "new_data2", "date")
bans = bans.annotate(new_data1=F("data3"), new_data2=F("data4")).values("new_data1", "new_data2", "date")
combined_queryset = signals | bans
combined_queryset = combined_queryset.order_by("date")
英文:

You can use .values() to get rid of the model layer and produce dicts containing the exact same fields.
You'll then be able to .union() to gather your data in a single queryset.
Then a simple .order_by() should do the trick.

signals = signals.annotate(data3=Value(""), data4=Value("")).values("data1", "data2", "data3", "data4", "date")
bans = bans.annotate(data1=Value(""), data2=Value("")).values("data1", "data2", "data3", "data4", "date")
combined_queryset = qignals | bans
combined_queryset = combined_queryset.order_by("date")

If data1 and data3 should be in the same column you can do

signals = signals.annotate(new_data1=F("data1"), new_data2=F("data2")).values("new_data1", "new_data2", "date")
bans = bans.annotate(new_data1=F("data3"), new_data2=F("data4")).values("new_data1", "new_data2", "date")
combined_queryset = qignals | bans
combined_queryset = combined_queryset.order_by("date")

答案2

得分: 0

你可以使用Django的查询集API来检索和组合Signal和Ban模型的数据,然后按日期对它们进行排序。

from django.shortcuts import render
from .models import Signal, Ban

def resume_view(request):
    signals = Signal.objects.order_by('-date')[:5]
    bans = Ban.objects.order_by('-date')[:5]

    data = sorted(list(signals) + list(bans), key=lambda x: x.date, reverse=True)

    context = {'data': data}
    return render(request, 'resume.html', context)

在你的HTML文件中:

{% for item in data %}
  <p>{{ item.__class__.__name__ }}{{ forloop.counter }} ({{ item.date|date:"d/m/Y" }})</p>
{% endfor %}
英文:

You can use Django's queryset API to retrieve and combine the data from the Signal and Ban models, and then sort them by date.

from django.shortcuts import render
from .models import Signal, Ban

def resume_view(request):
    signals = Signal.objects.order_by(&#39;-date&#39;)[:5]
    bans = Ban.objects.order_by(&#39;-date&#39;)[:5]

data = sorted(list(signals) + list(bans), key=lambda x: x.date, reverse=True)

context = {&#39;data&#39;: data}
return render(request, &#39;resume.html&#39;, context)

In your html:

{% for item in data %}
  &lt;p&gt;{{ item.__class__.__name__ }}{{ forloop.counter }} ({{ item.date|date:&quot;d/m/Y&quot; }})&lt;/p&gt;
{% endfor %}

huangapple
  • 本文由 发表于 2023年3月8日 19:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定