2
from django.contrib.auth.models import User

class Drink(models.Model):
        owner = models.ForeignKey(User)
        name = models.CharField(max_length=50)

ユーザーのログアウト後にすべてのドリンクを削除する方法は?

def delete_drink(request):
    user = request.user
    drink = Drink.objects.filter(owner=user)
    drink.delete()
4

1 に答える 1

1

これはうまくいくかもしれません:

from django.contrib.auth.signals import user_logged_out

def delete_drink(user):
    drink = Drink.objects.filter(owner=user)
    drink.delete()

def on_logout(sender, **kwargs):
    if kwargs['user']:
        delete_drink(kwargs['user'])

user_logged_out.connect(on_logout, dispatch_uid="user_logout1")
于 2012-10-13T19:27:17.713 に答える