Usage¶
Use anon.BaseAnonymizer
to define your anonymizer classes:
import anon
from your_app.models import Person
class PersonAnonymizer(anon.BaseAnonymizer):
email = anon.fake_email
# You can use static values instead of callables
is_admin = False
class Meta:
model = Person
# run anonymizer: be cautious, this will affect your current database!
PersonAnonymizer().run()
Built-in functions¶
import anon
anon.fake_word(min_size=_min_word_size, max_size=20)
anon.fake_text(max_size=255, max_diff_allowed=5, separator=' ')
anon.fake_small_text(max_size=50)
anon.fake_name(max_size=15)
anon.fake_username(max_size=10, separator='')
anon.fake_email(max_size=40, suffix='@example.com')
anon.fake_url(max_size=50, scheme='http://', suffix='.com')
anon.fake_phone_number(format='999-999-9999')
Lazy attributes¶
Lazy attributes can be defined as inline lambdas or methods, as shown below,
using the anon.lazy_attribute
function/decorator.
import anon
from your_app.models import Person
class PersonAnonymizer(anon.BaseAnonymizer):
name = anon.lazy_attribute(lambda o: 'x' * len(o.name))
@lazy_attribute
def date_of_birth(self):
# keep year and month
return self.date_of_birth.replace(day=1)
class Meta:
model = Person
The clean method¶
import anon
class UserAnonymizer(anon.BaseAnonymizer):
class Meta:
model = User
def clean(self, obj):
obj.set_password('test')
obj.save()
Defining a custom QuerySet¶
A custom QuerySet can be used to select the rows that should be anonymized:
import anon
from your_app.models import Person
class PersonAnonymizer(anon.BaseAnonymizer):
email = anon.fake_email
class Meta:
model = Person
def get_queryset(self):
# keep admins unmodified
return Person.objects.exclude(is_admin=True)
High-quality fake data¶
In order to be really fast, django-anon uses it’s own algorithm to generate fake data. It is really fast, but the generated data is not pretty. If you need something prettier in terms of data, we suggest using Faker, which can be used out-of-the-box as the below:
import anon
from faker import Faker
from your_app.models import Address
faker = Faker()
class PersonAnonymizer(anon.BaseAnonymizer):
postalcode = faker.postalcode
class Meta:
model = Address