{"id":15024454,"url":"https://github.com/mostafa-mahmoud/djenerator","last_synced_at":"2025-10-23T18:52:27.848Z","repository":{"id":57422780,"uuid":"2260566","full_name":"mostafa-mahmoud/djenerator","owner":"mostafa-mahmoud","description":"A simple django tool for generating random/customized test data for django model descriptions!","archived":false,"fork":false,"pushed_at":"2022-10-02T15:12:33.000Z","size":252,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T01:51:13.494Z","etag":null,"topics":["constraint-satisfaction-problem","django","django-application","random-generation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mostafa-mahmoud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-08-24T09:03:29.000Z","updated_at":"2022-12-23T12:38:03.000Z","dependencies_parsed_at":"2022-08-27T05:25:22.006Z","dependency_job_id":null,"html_url":"https://github.com/mostafa-mahmoud/djenerator","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mostafa-mahmoud%2Fdjenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mostafa-mahmoud%2Fdjenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mostafa-mahmoud%2Fdjenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mostafa-mahmoud%2Fdjenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mostafa-mahmoud","download_url":"https://codeload.github.com/mostafa-mahmoud/djenerator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248530420,"owners_count":21119587,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constraint-satisfaction-problem","django","django-application","random-generation"],"created_at":"2024-09-24T20:00:22.919Z","updated_at":"2025-10-23T18:52:22.804Z","avatar_url":"https://github.com/mostafa-mahmoud.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Djenerator\n\nDjenerator is a simple tool made to consistently generate test, random or dummy data from the model descriptions of Django. Djenerator has several advantages\nover other alternatives:\n\n1. 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).\n2. It generates data for dependent models (based on related fields), for example, if a model A has a `ForeignKey` to model B, then djenerator will recognize that\ngenerating data for model A requires having some instances of model B, and will\ngenerate them when necessary.\n3. Generators can simultaneously satisfy many constraints, like the `unique` flag,\n`null` flag, `unique_together` constraints, or [django field validators](https://docs.djangoproject.com/en/3.2/ref/validators/).\n4. Easy to extend to include your own values for some of the fields.\n5. It can generate a big dump for a database at once, not only individual models.\n\n\n## Installation\n\n1. Using pip, you can install djenerator using this command:\n\n```bash\n$ pip3 install djenerator\n```\n\n2. Add `'djenerator'` to your `INSTALLED_APPS`:\n\n```python\nINSTALLED_APPS = [\n   'django.contrib.admin',\n   'django.contrib.auth',\n   'django.contrib.contenttypes',\n   'django.contrib.sessions',\n   # ...\n   'djenerator',\n   # ...\n]\n```\n\n## Usage\n\nThe generation of data can be done by a command using the `manage.py` file in your project:\n\n```bash\n$ python3 manage.py jenerate app_name size\n```\n\nEquivalently, this can be done within python code\n\n```python\nfrom djenerator import generate_test_data\ngenerate_test_data(app_name, size)\n```\n\n### To generate for specific models\n\n```bash\n$ python3 manage.py jenerate app_name size --models ModelA ModelB ...\n```\n\nEquivalently, this can be done within python code\n\n```python\nfrom djenerator import generate_test_data\ngenerate_test_data(app_name, size, models_cls=[\"ModelA\", \"ModelB\"])\n```\n\n### To allow some null values\n\nBy default, djenerator generates data for all fields even if null values are allowed. To allow some null values, allow the following option:\n\n```bash\n$ python3 manage.py jenerate app_name size --allow-null\n```\n\nEquivalently, this can be done within python code\n\n```python\nfrom djenerator import generate_test_data\ngenerate_test_data(app_name, size, allow_null=True)\n```\n\n## Writing your custom generators\n\nYou can add a customized values generator for a some fields in some models.\n**This will most likely be required if you are writing your own validators.**\nWriting your customized generator, can be done by adding a class to the module `{app_name}.test_data`:\n1. Write a class with the same name as the model.\n2. For the fields to write a custom generator, write an attirbute with a matching name, with the generator as a value.\n\nThe generator is either a function with no any required arguments that generates random values, or can also be an iterable of all possible values.\n\nAs an example, in an app `testapp`, if `testapp/models.py` is:\n\n```python\nfrom django.core.exceptions import ValidationError\nfrom django.db import models\nfrom django.utils.translation import gettext_lazy as _\n\n\ndef validate_mod91(value):\n    if value % 91 != 0:\n        raise ValidationError(\n            _('%(value)s is not an even number'), params={'value': value},\n        )\n\n\nclass TestModel(models.Model):\n    field1 = models.CharField(max_length=256)\n    field2 = models.BigIntegerField(validators=[validate_mod91])\n    field3 = models.IntegerField()\n\n    class Meta:\n        unique_together = (('field1', 'field2'),)\n```\n\nthen `testapp/test_data.py` will contain the following:\n\n```python\nimport random\n\nclass TestModel:\n    field2 = lambda: random.randint(1, 1000) * 91\n    field3 = list(range(10000))\n```\n\nThe value generator does not necessarily need to always generate unique or valid values, however, it should generate them with high probability.\nIn the example above, the `validate_mod91` checks if a number is divisible by 91, two bad generators can be:\n1. `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.\n1. `lambda: random.randint(0, 10) * 91` will 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 are `unique` or many `unique_together` constraints).\n\n\n## Running the tests\n\nRun the tests by running the command:\n\n```bash\n$ python3 manage.py test\n```\n\nThe following combinations are tested:\n\n| Django      | Python      | Status |\n| ----------- | ----------- | ------ |\n|    1.10.8   |     3.5     |   ✅   |\n|    1.11.29  |     3.5     |   ✅   |\n|    1.11.29  |     3.6     |   ✅   |\n|    1.11.29  |     3.7     |   ✅   |\n|    2.2.28   |     3.5     |   ✅   |\n|    2.2.28   |     3.6     |   ✅   |\n|    2.2.28   |     3.7     |   ✅   |\n|    3.2.15   |     3.6     |   ✅   |\n|    3.2.15   |     3.7     |   ✅   |\n|    4.0.7    |     3.8     |   ✅   |\n|    4.0.7    |     3.9     |   ✅   |\n|    4.1.1    |     3.8     |   ✅   |\n|    4.1.1    |     3.9     |   ✅   |\n\n## Requirements\n\n1. `django \u003e= 1.10`.\n1. `pytz` is required to be manually installed for `django \u003c 1.11`, otherwise it is installed by django when it is required (it is not required for some higher versions of django).\n1. `pillow` if ImageFields are used, we don't require it be default, but django will.\n\nOur setup requires only `django`, other packages are reported by django.\n\n## TODOs and BUGS\n\nSee: https://github.com/mostafa-mahmoud/djenerator/issues\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmostafa-mahmoud%2Fdjenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmostafa-mahmoud%2Fdjenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmostafa-mahmoud%2Fdjenerator/lists"}