django admin - 选择单个模型发布到站点

发布于 2024-08-30 01:08:47 字数 325 浏览 5 评论 0原文

嘿,我正在用 django 制作一个带有附加Shoutbox的弹出式广播播放器,但我无法获得我想要的管理功能。

我有两个模型:

1)一个流(代表管理员可以发布在首页上播放的某个广播流 - 即有多个保存的流,但一次只有一个在首页上播放,由管理员自行决定)管理员)

2)喊叫(已输入喊叫箱的喊叫,并与特定流相关联,即每个流都有由网站用户输入的多个喊叫。)

我希望管理员能够登录后端,创建多个流,但每次只能选择一个发布。据推测,每个流都应该有一个属性(即 is_published),我应该创建一个管理操作来对每个流执行检查,并仅发布正确的流?这是正确的方法还是我错过了什么

Hey, I'm making a popup radio player with an attached shoutbox with django and i'm having trouble getting the admin functionality I want.

I have two models:

1) A Stream (which represents a certain radio stream that the admin can publish to play on the frontpage - i.e. there are multiple saved streams, but only one is playing on the frontpage at a time, at the discretion of the admin)

2) A Shout (a shout that has been entered into the shoutbox, and is associated to a certain stream, i.e. every stream has multiple shouts that are entered by users of the site.)

I want the admin to be able to log into the back end, create multiple streams, but only select one to be published at any one time. Presumabley each stream should have an attribute (i.e is_published) and I should create an admin action to perform a check of every stream, and publish only the correct one? Is this the correct way to go about it or am I missing something

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

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

发布评论

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

评论(1

如梦亦如幻 2024-09-06 01:08:47

我预见到的唯一潜在问题是,如果有人在管理员更改流之前已经连接并正在收听流,该怎么办?该人应该听到新的流还是继续听到他们正在听的流?

除此之外,按照你所描述的方式,我可以看到它的工作原理。您可以创建一个始终返回当前流的 url/view,例如 /stream/current/。该 URL 的视图将始终获得最新的 Stream 模型...

def current_stream(request, *args, **kwargs):
   # Get first stream marked as published
   s = Stream.objects.filter(is_published=True)[1][0]
   return do_streaming_stuff(s)

因为您可能会在应用程序的其他地方使用“将此流设置为活动流”,所以您可以将其设为Stream 模型的一部分...

class Stream(models.Model):
    is_published = models.BooleanField()

    def set_as_active_stream(self, do_save=True):
        enabled_streams = Stream.objects.filter(is_published=True)
        for s in enabled_streams:
            s.is_published=False
            s.save()    
        if do_save:
            self.is_published=True
            self.save()

    def save(self, *args, **kwargs):
        if self.is_published:
            # No need to double save, since we're already saving it
            self.set_as_active_stream(do_save=False)
        super(Stream, self).save(*args, **kwargs)

The only potential problem I forsee is, what if someone has already connected and is listening to a stream before the admin changes it. Should that person hear the new stream or continue to hear the stream they were listening to?

Other than that, the way you've described it, I could see it working. You could make a url/view which always returns the current stream, like /stream/current/. The view for that URL will always get the most current Stream model...

def current_stream(request, *args, **kwargs):
   # Get first stream marked as published
   s = Stream.objects.filter(is_published=True)[1][0]
   return do_streaming_stuff(s)

Because you're going to probably use the "set this stream as active stream" elsewhere in your app, you could make it a part of your Stream model...

class Stream(models.Model):
    is_published = models.BooleanField()

    def set_as_active_stream(self, do_save=True):
        enabled_streams = Stream.objects.filter(is_published=True)
        for s in enabled_streams:
            s.is_published=False
            s.save()    
        if do_save:
            self.is_published=True
            self.save()

    def save(self, *args, **kwargs):
        if self.is_published:
            # No need to double save, since we're already saving it
            self.set_as_active_stream(do_save=False)
        super(Stream, self).save(*args, **kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文