Skip to content

Instantly share code, notes, and snippets.

@robertpro
Created June 22, 2019 00:28
Show Gist options
  • Save robertpro/6e2a06dc3a7a9b2b96a8781e92b8b507 to your computer and use it in GitHub Desktop.
Save robertpro/6e2a06dc3a7a9b2b96a8781e92b8b507 to your computer and use it in GitHub Desktop.
Minimal script for autogeneration of factory-boy factories.
import django
django.setup()
from django.apps import apps
models = apps.get_app_config('app').get_models()
for model in models:
fields = [field.name for field in m._meta.fields if field.name != 'id']
fields_code = ''
for i in range(len(fields)):
field = fields[i]
if i == 0:
fields_code += f' {field} = 0'
else:
fields_code += f'\n {field} = 0'
print(f"""
class {m._meta.object_name}Factory(DjangoModelFactory):
class Meta:
model = {m._meta.object_name}
{fields_code}
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment