过滤通知'删除

时间:2015-11-18 17:39:29

标签: python django notifications

我的通知系统,有选项"删除",就像" make as read",然后我希望当用户执行操作" read",只是从他的通知中删除,而一般用户没有这个通知。

Model.py

class Notificaciones(models.Model):
    user = models.ManyToManyField(MiUsuario)
    Tipo_de_notificaciones = ( (1,'Ofertas'),(2,'Error'),(3,'Informacion'))
    Tipo = models.IntegerField('Tipo de notificacion',choices=Tipo_de_notificaciones, default=3,)
    titulo = models.CharField("Nombre de la notifiacion",max_length=50)
    mensaje = models.TextField("Descripcion de la notificacion")
    imagen = models.ImageField("Imagen de la notificacion",upload_to="notificaciones")
    Fecha_Caducidad_notificacion = models.DateField("Fecha de caducidad",auto_now=False,auto_now_add=False)
    Estado = models.BooleanField("Estado de la notificacion", default=False)

以及我在哪里登录删除发生的事情

views.py

def delete_notificaciones(request, notificaciones_id):
    notifi = Notificaciones.objects.get(id=notificaciones_id)
    notifi = Notificaciones.objects.filter(user=request.user.id)
    notifi.Estado = True
    for notifi in notifi:
        notifi.save()
    return HttpResponseRedirect('/')

URL.py

url(r'^delete/(?P<notificaciones_id>\d+)/$', 'app.views.delete_notificaciones', name='Vulpini.co'),

为什么不过滤用户并发出类似读取的通知。

1 个答案:

答案 0 :(得分:0)

notifi = Notificaciones.objects.filter(user=request.user.id)
模型中的

user字段是一个对象,而不是字段,所以你应该像这样过滤:

notifi = Notificaciones.objects.filter(user=request.user)

我认为你应该改变一下你的观点。我想您通过将Estado更改为True来进行软删除。

以下代码会将所有用户的通知标记为已读:

def delete_notificaciones(request, notificaciones_id):
    notifi = Notificaciones.objects.get(id=notificaciones_id)  # I don't know why you get this object
    notifi = Notificaciones.objects.filter(user=request.user)  # Here you get a QuerySet, not an object

    # use update instead of iterate over the QuerySet

    notifi.update(Estado=True)

    return HttpResponseRedirect('/')

如果您想将ID通知传递的当前标记为只读,则应该执行与以下内容类似的操作:

def delete_notificaciones(request, notificaciones_id):
    notifi = Notificaciones.objects.get(pk=notificaciones_id, user=request.user)
    notifi.Estado = True
    notifi.save()
    return HttpResponseRedirect('/')
相关问题