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()
答案 0 :(得分: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")