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 回答