Package: djenerator
DjeneratorDjenerator is a simple tool made to consistently generate test, random or dummy data from the model descriptions of Django. Djenerator has several advantages
over other alternatives:Entry level is extremely easy, just install the package and include it in your project, there's no requirement by the user to specify the random data factory (it is optional).It generates data for dependent models (based on related fields), for example, if a model A has aForeignKeyto model B, then djenerator will recognize that
generating data for model A requires having some instances of model B, and will
generate them when necessary.Generators can simultaneously satisfy many constraints, like theuniqueflag,nullflag,unique_togetherconstraints, ordjango field validators.Easy to extend to include your own values for some of the fields.It can generate a big dump for a database at once, not only individual models.InstallationUsing pip, you can install djenerator using this command:$pip3installdjeneratorAdd'djenerator'to yourINSTALLED_APPS:INSTALLED_APPS=['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions',# ...'djenerator',# ...]UsageThe generation of data can be done by a command using themanage.pyfile in your project:$python3manage.pyjenerateapp_namesizeEquivalently, this can be done within python codefromdjeneratorimportgenerate_test_datagenerate_test_data(app_name,size)To generate for specific models$python3manage.pyjenerateapp_namesize--modelsModelAModelB...Equivalently, this can be done within python codefromdjeneratorimportgenerate_test_datagenerate_test_data(app_name,size,models_cls=["ModelA","ModelB"])To allow some null valuesBy default, djenerator generates data for all fields even if null values are allowed. To allow some null values, allow the following option:$python3manage.pyjenerateapp_namesize--allow-nullEquivalently, this can be done within python codefromdjeneratorimportgenerate_test_datagenerate_test_data(app_name,size,allow_null=True)Writing your custom generatorsYou can add a customized values generator for a some fields in some models.This will most likely be required if you are writing your own validators.Writing your customized generator, can be done by adding a class to the module{app_name}.test_data:Write a class with the same name as the model.For the fields to write a custom generator, write an attirbute with a matching name, with the generator as a value.The generator is either a function with no any required arguments that generates random values, or can also be an iterable of all possible values.As an example, in an apptestapp, iftestapp/models.pyis:fromdjango.core.exceptionsimportValidationErrorfromdjango.dbimportmodelsfromdjango.utils.translationimportgettext_lazyas_defvalidate_mod91(value):ifvalue%91!=0:raiseValidationError(_('%(value)sis not an even number'),params={'value':value},)classTestModel(models.Model):field1=models.CharField(max_length=256)field2=models.BigIntegerField(validators=[validate_mod91])field3=models.IntegerField()classMeta:unique_together=(('field1','field2'),)thentestapp/test_data.pywill contain the following:importrandomclassTestModel:field2=lambda:random.randint(1,1000)*91field3=list(range(10000))The value generator does not necessarily need to always generate unique or valid values, however, it should generate them with high probability.
In the example above, thevalidate_mod91checks if a number is divisible by 91, two bad generators can be:lambda: random.randint(0, 100000)will generate valid values but with only 1% chance; however, a chance higher than 20% or even 50% would be much better.lambda: random.randint(0, 10) * 91will generate only 11 unique valid values; however, it is recommended to return a factor higher than the total number of models to be generated (especially if there areuniqueor manyunique_togetherconstraints).Running the testsRun the tests by running the command:$python3manage.pytestThe following combinations are tested:DjangoPythonStatus1.10.83.5✅1.11.293.5✅1.11.293.6✅1.11.293.7✅2.2.283.5✅2.2.283.6✅2.2.283.7✅3.2.153.6✅3.2.153.7✅4.0.73.8✅4.0.73.9✅4.1.13.8✅4.1.13.9✅Requirementsdjango >= 1.10.pytzis required to be manually installed fordjango < 1.11, otherwise it is installed by django when it is required (it is not required for some higher versions of django).pillowif ImageFields are used, we don't require it be default, but django will.Our setup requires onlydjango, other packages are reported by django.TODOs and BUGSSee:https://github.com/mostafa-mahmoud/djenerator/issues