django-anon
raw JSON → 0.3.2 verified Mon Apr 27 auth: no python
django-anon is a Django library that helps anonymize production data so it can be safely used in non-production environments. It provides model mixins and field anonymization functions. The current version is 0.3.2. Release cadence is sporadic; last release was in 2019.
pip install django-anon Common errors
error django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. ↓
cause Calling django_anon functions without setting up Django settings first.
fix
Add import os; os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings'); import django; django.setup() before using django-anon.
error AttributeError: module 'django_anon' has no attribute 'AnonymousUser' ↓
cause Trying to import from the wrong path, or the library is not installed correctly.
fix
Use correct import: from django_anon import AnonymousUser. Verify installation with 'pip list | grep django-anon'.
Warnings
gotcha If you override save() in your model and call anonymize(), ensure you call super() to avoid infinite loops. Version 0.3.2 fixed a similar infinite loop in fake_username with empty separator. ↓
fix Always call super().save() after anonymization in custom save methods.
deprecated The built-in bulk_update method changed in Django 2.2. django-anon 0.3 uses Django's built-in method if available, but there may be edge cases. ↓
fix Use Django's native bulk_update instead of relying on django-anon's version.
breaking The fake_email function default max_size changed from infinite to 40 in version 0.3. This can cause emails to be truncated if you expect longer ones. ↓
fix Set max_size explicitly when calling fake_email if you need longer addresses.
Imports
- AnonymousUser wrong
from django_anon.models import AnonymousUsercorrectfrom django_anon import AnonymousUser - AnonymizationMixin
from django_anon.mixins import AnonymizationMixin - fake_email
from django_anon.fields import fake_email
Quickstart
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
import django
django.setup()
from django_anon import AnonymousUser
from myapp.models import User
for user in User.objects.all()[:1]:
anon_user = AnonymousUser(user)
print(anon_user.email)
print(anon_user.username)