2012-10-13 54 views
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() 
+1

A信號coould滿足您的需求。 http://stackoverflow.com/questions/1990502/django-signal-when-user-logs-in – Jingo

回答

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") 
相關問題