簡體   English   中英

如何使用swampdragon在Django中實現用戶特定的實時通知?

[英]How to implement user specific real time notifications in django using swampdragon?

我一直在嘗試在項目中實現用戶特定的實時通知。 盡管我能夠廣播實時通知,但我仍在努力創建特定於用戶的頻道,將選定的通知發送給用戶。 就我所做的相同嘗試而言,以下是使用swampdragon-auth查看幾個示例后編寫的代碼段:

routers.py:

class NotificationRouter(ModelPubRouter):
valid_verbs = ['subscribe']
route_name = 'notifications'
model = Notification
serializer_class = NotificationSerializer

def get_query_set(self, **kwargs):
    return self.model.objects.filter(user=self.connection.user)

def get_object(self, **kwargs):
    return self.model.objects.get(user=self.connection.user, pk=kwargs['pk'])

def get_subscription_contexts(self, **kwargs):
    return {'user__id': self.connection.user.pk}

models.py

class Notification(SelfPublishModel, models.Model):
serializer_class = NotificationSerializer
message = models.TextField()
verb = models.CharField(null=True, default="achieved", max_length=20)
user = models.ForeignKey(settings.AUTH_USER_MODEL)

serializer.py

class NotificationSerializer(ModelSerializer):
    class Meta:
        model = 'notifications.Notification'
        publish_fields = ['message','verb','user']

此代碼無法實現特定於用戶的通知執行,並向所有用戶廣播消息。 據我所知,get_subscription_contexts是用於過濾對選定用戶的通知的函數,但是我無法使用此代碼來實現。 如果有人可以幫助我解決這個問題,那就太好了。

謝謝

人士Himanshu

在一個示例項目中測試您的代碼,並添加兩個用戶,我收到的通知僅是我的登錄用戶所期望的。

您可以確認您使用的SwampDragon版本嗎?

經過測試:

  • Django的== 1.7
  • SwampDragon == 0.4.2
  • SwampDragon-auth的== 0.1.3

我已經創建了一個具有上述實現的演示項目-https: //github.com/CptLemming/swampdragon_notification_example

在此示例中,我可以登錄Django管理員,創建通知,並使通知僅針對所選用戶顯示。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM