Django 2.2普通用户自助删除账号的最佳方案咨询
Best Approach for Regular User Account Deletion in Django 2.2
Great question—balancing Django's official is_active best practice with keeping your database clean as your user base grows is a common challenge. Here’s a robust, compliant strategy tailored for regular users (non-staff/superusers):
1. Soft Delete First, Hard Cleanup Later
Django 2.2 docs recommend setting
is_active=Falseinstead of deleting accounts to preserve foreign key relationships. We’ll build on this while adding a cleanup mechanism:
- Add a
deleted_atDateTimeField to your User model (extend the default or use a custom one) to track when the account was marked inactive. Run this migration:from django.db import migrations, models from django.utils import timezone class Migration(migrations.Migration): dependencies = [ ('auth', '0011_update_proxy_permissions'), ] operations = [ migrations.AddField( model_name='user', name='deleted_at', field=models.DateTimeField(null=True, blank=True, default=None), ), ] - When a regular user requests deletion, immediately set
user.is_active = Falseanduser.deleted_at = timezone.now(), then save the user. This keeps related data intact. - Set up a periodic task (use Celery, Django Cron, or even a simple management command run via server cron) to hard-delete eligible users on a schedule (e.g., monthly). The task should:
- Filter for users where
is_active=False,deleted_atis older than your chosen retention period (e.g., 30 days), andis_staff=False+is_superuser=False - Permanently delete these users to reduce database bloat.
- Filter for users where
2. Build a Secure User-Initiated Deletion Flow
- Create an authenticated view for users to request deletion, with these safeguards:
- Require re-authentication (ask for their password) to prevent accidental or unauthorized requests.
- Display a clear confirmation explaining the process: immediate deactivation, permanent deletion after [X] days, and what data will be removed/anonymized.
- After confirmation, update the user’s
is_activeanddeleted_atfields as mentioned above.
3. Ensure Privacy Compliance
- Before hard-deleting, scrub or anonymize personal data to meet regulations like GDPR:
- Replace identifiable fields (email, full name) with anonymized values (e.g.,
deleted_user_123@example.com,Anonymous User) - Keep only necessary non-identifying data if required for auditing or legal purposes.
- Replace identifiable fields (email, full name) with anonymized values (e.g.,
- Offer a manual override for users who request immediate permanent deletion (if allowed by local laws) — skip the retention period and delete their account right after deactivation.
4. Auto-Exclude Inactive Users from Queries
- Create a custom user manager to automatically filter out inactive users from most application queries:
from django.contrib.auth.models import UserManager class ActiveUserManager(UserManager): def get_queryset(self): return super().get_queryset().filter(is_active=True) # In your custom User model: from django.contrib.auth.models import AbstractUser class User(AbstractUser): deleted_at = models.DateTimeField(null=True, blank=True) objects = UserManager() # Use for admin/backend where you need all users active_objects = ActiveUserManager() # Use for frontend/user-facing views - Use
User.active_objects.all()instead ofUser.objects.all()in most views to avoid showing inactive users to other users or in search results.
内容的提问来源于stack exchange,提问作者e rosario
相关产品推荐
相关产品推荐

