I am currently trying to create script where once the data has been deleted from django admin, we can add the data into database again for testing purposes.
In django when we are adding data, we need to fill every field in django admin but what i want is a script in which I have some test data and when I run, it automatically adds it to the database without the need of django admin.
How can I achieve this?
CodePudding user response:
To fill up your target, you can use Faker. You need to create a python script corresponding to your model fields mapping and assign the fake values to your model (like creating a general model object). By this, you can create one object at a time. To create multiple objects at a time you can use for loop.
Example:
from faker import Faker
fake = Faker()
Author.objects.create(
email=self.email,
date_of_birth=self.fake.email()
)