How to use Fixtures in Django
Today, I will talk to you about how to use Fixtures in Django. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Basic configuration
Configure the following in settings.py:
FIXTURE_DIRS = ('/ path/to/api/fixtures/',)
In the app to be tested, add the directory fixtures
Under the newly added fixtures directory, create a new file myapp.json with a name of your own and a suffix of .json.
Configure myapp.json
The format of the content in the file is as follows. In actual use, delete the actual situation, use data, and delete comments. If you need more than one piece of data, you can write more {} content:
[{
"model": "myapp.user", # database name
"Competition": "a864340d850f484bb89ea2981047bc5e", # data to be imported
"fields": {# the field to be imported. The field name is the same as the field in the database (models). The value is set according to the actual needs.
"username": "zhangsan"
User_num: 00000
"zh_name": "Zhang San"
"mobile": "187 12345678"
"mail": "zhangsan@abc.com"
"is_deleted": false
"create_time": "2013-01-16"
"update_time": "2013-01-16"
}
}
]
Configure tests.pyclass Test_user_name (TestCase):
'' whether the tester name is normal''
Fixtures = ['myapp.json'] # Select the user data you want to import to test here
Def test_user_name (self):
User = User.objects.get (username='zhangsan')
Self.assertEqual (user.name, "zhangsan")
After reading the above, do you have any further understanding of how to use Fixtures in Django? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.