I created a function in my settings.py file called is_debug that checks for an environment variable we pass to it - which in our case is DJANGO_DEBUG we check that in our commands to hopefully prevent these commands from ever being ran in a production environment.write ( """ !!!We will not delete the app data because DEBUG is set to False which may indicate a production environment!!! """ )Ī few additional notes about these commands: write ( "Deleting Things from our app" ) models = for m in models : m. get ( "DJANGO_DEBUG" )): # Delete Things from our app self. atomic def handle ( self, * args, ** kwargs ): # Check to ensure we are in an environment # where debug is True to avoid deleting data # from a production environment if is_debug ( os. Our create_app_data.py should contain something like this:įrom your_ttings import is_debug from your_app.models import Thing class Command ( BaseCommand ): help = "Deletes dummy Things for our app" # Using this decorator ensures that the SQL is bundled up and the commands are # ran together - speeding up the process. Now we have added two commands to our project - so now we can write what they will do! STEP 2: Inside the commands directory create the following two files: STEP 1: In your app directory create the following directories Generating our own custom management commands is easy in Django! I am going to create a command we can call with python manage.py create_app_data It can generate almost any random data you would want - check out the The factory_boy docs for more information and examples on how it generates that data. Now when we call the factory like ThingFactory() it will generate a Thing model for us with random data. Faker ( "last_name" ) username = factory. Faker ( "first_name" ) last_name = factory. now ()) end_year = 2021 first_name = factory. DjangoModelFactory ): class Meta : model = yourapp. STEP 3: Create our "factories" (examples below of a Django model and the corresponding factory) Standard Django Model( models.py):Ĭlass ThingFactory ( factory. This file will give us all the control we need so our dummy data is realistic and randomized, just like it would be in the real world. STEP 2: Create a factories.py file ( important: ensure it is located in your app directory) If you're not using requirements files - perhaps read my article on what are requirements files. I prefer to add factory_boy to my requirements-dev.txt file at this point - to make it easy on myself in the future. STEP 1: From console run pip install factory_boy With the assumption that you have a working application with existing models - let's add factory_boy to the mix so we can get some dummy data generated for us as needed. Open your console and navigate to your Django project directory Adding Factories with Factory Boy Open your Django project in your favorite IDE/editor (I prefer VSCode, if you like something else - leave a comment!) This all gets broken down into three main sections: creating the factories, creating the commands and running the commands. It does such a great job of giving us realistic data for our models without a lot of hassle - and once again, we'll lean on that library here for our dummy data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |