django restframework如何计算对象并获取自(时间戳)?

时间:2018-11-24 23:23:41

标签: python django django-rest-framework

伙计们。我试图通过django restframework创建一个通知系统,我的问题是我不知道如何获取未读的通知计数,或者不通过django restframework将时间戳记格式设置为since(timestamp),因为在正常情况下我完全知道该怎么做。

这是我的模型:

class Notificaciones(models.Model):
   user_a_notificar = models.ForeignKey(settings.AUTH_USER_MODEL, 
   on_delete=models.CASCADE)
   komentario = models.OneToOneField(Post,on_delete=models.CASCADE,  
   blank=True, related_name="komentario")
   estado = models.ManyToManyField('Evento', blank=True)

def __str__(self):
    return self.user_a_notificar.username

class Evento(models.Model):
    creadores = models.ManyToManyField(settings.AUTH_USER_MODEL,  
    blank=True, related_name="generadores")
    status = models.CharField(default="Unread", max_length=10)
    mensaje = models.CharField(max_length=200)
    event = models.CharField(max_length=15)
    noti_de_evento = models.ForeignKey(Notificaciones, 
    on_delete=models.CASCADE)
    timestampe = models.DateTimeField(auto_now=False, auto_now_add=True)



def __str__(self):
    return self.mensaje

我的观点:

class Notifi(APIView):
    serializer = notizerializer
    def get(self, request, format=None):
        noti = Notificaciones.objects.filter(user_a_notificar=request.user.id)
        lista_noti = Evento.objects.filter(noti_de_evento__in=noti)
        lista_noticount = Evento.objects.filter(noti_de_evento__in=noti, status="Unread").count()
        response = self.serializer(lista_noti, many=True)
        return HttpResponse(json.dumps(response.data))

最后是我的序列化器:

class notizerializer(ModelSerializer):

    class Meta:
        model = Evento
        fields = ('status', 'mensaje', 'event', 'timestampe', 'id')

2 个答案:

答案 0 :(得分:0)

这是我的两分钱

这是一个model,因此您可以了解如何获取unread事件:

class Notificaciones(models.Model):
    user_a_notificar = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    komentario = models.OneToOneField(Post, on_delete=models.CASCADE, blank=True, related_name="komentario")
    estado = models.ManyToManyField('Evento', blank=True)

    @property
    def unread(self):
        return self.evento_set.filter(status="Unread").count()

    def __str__(self):
        return self.user_a_notificar.username

使用相同的serializer和视图,但实际上在您看来,您正在返回Evento而不是Notificaciones的序列化器。

要获得since(timestamp)是相同的想法,只需创建一个将计算该属性的属性即可。然后,在两种情况下,您都可以将模型的属性添加到序列化器中。

答案 1 :(得分:0)

这是更详细的答案:

from django.utils import timesince

class Notificaciones(models.Model):
    user_a_notificar = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    komentario = models.OneToOneField(Post, on_delete=models.CASCADE, blank=True, related_name="komentario")
    estado = models.ManyToManyField('Evento', blank=True)

    def __str__(self):
        return self.user_a_notificar.username

class Evento(models.Model):
    creadores = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name="generadores")
    status = models.CharField(default="Unread", max_length=10)
    mensaje = models.CharField(max_length=200)
    event = models.CharField(max_length=15)
    noti_de_evento = models.ForeignKey(Notificaciones, on_delete=models.CASCADE)
    timestampe = models.DateTimeField(auto_now=False, auto_now_add=True)

    @property
    def unread(self):
        return sum([n.evento_set.filter(status="Unread").count()
                    for n in self.notificaciones_set.all()])

    @property
    def timesince(self):
        return timesince.timesince(self.timestampe)

    def __str__(self):
        return self.mensaje

但是在序列化器中:

from rest_framework import serializers
from .models import Evento


class notizerializer(serializers.ModelSerializer):
    unread = serializers.IntegerField()
    timesince = serializers.DateTimeField()

    class Meta:
        model = Evento
        fields = ('status', 'mensaje', 'event', 'timestampe', 'id', 'unread', 'timesince')

这是我来自假数据的结果:

[{"status": "Unread", "mensaje": "jaja", "event": "1", "timestampe": "2018-11-25T01:48:17.374179Z", "id": 1, "unread": 1, "timesince": "16\u00a0hours, 41\u00a0minutes"}]