Django 民意调查新记录

发布于 2024-09-29 02:11:59 字数 95 浏览 2 评论 0原文

在 ajax 中,我正在轮询 django url 以检索最新记录。我不想显示之前检索到的任何记录,并且只想为每个轮询请求检索 1 条记录。

最好的方法是什么?

In ajax I am polling a django url to retrieve the latest records. I do not want to display any records I have retrieved previously and I only want to retrieve 1 record for each poll request.

What would be the best way to do this?

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

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

发布评论

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

评论(2

懵少女 2024-10-06 02:11:59
class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    expire_date = models.DateField()
    class Meta:
        get_latest_by = 'pub_date'

>>> from mysite.models import Article
>>> Article.objects.latest()

如果我对你的问题的理解没有错误,你可以使用 Metaclass 的 get_latest_by 属性,并调用可以满足你的目的的方法latest()`,以便为了不检索记录两次,您可以使用 obj.pk > your_prev_retired_pk。

class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    expire_date = models.DateField()
    class Meta:
        get_latest_by = 'pub_date'

>>> from mysite.models import Article
>>> Article.objects.latest()

If I'm not wrong in understanding your question, You may go for get_latest_by attribute ofMetaclass and call the methodlatest()` which may serve your purpose, in order not to retrieve the record twice you may use the obj.pk > your_prev_retired_pk.

紫罗兰の梦幻 2024-10-06 02:11:59

唔。我可以立即想到两种方法来做到这一点——肯定还有更多。

您可以添加一个名为“already_retrieved”的字段,并将其设置为 True 对于那些已经检索到的字段,然后只获取Whatever.objects.filter(already_retrieved=False)。

另外,如果它们按 pk 排序,您可以跟踪自己在 pk 列表中的位置。

Hmm. You could do it two ways that I can think of off the bat - there are surely more.

You can add a field called "already_retrieved" and set it to True for those fields that have already been retrieved, and then only grab Whatever.objects.filter(already_retrieved=False).

Also, if they are in order by a pk, you could just keep track of how far you are in the list of pk's.

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