美文网首页
django初始化数据yaml

django初始化数据yaml

作者: abelweiwencai | 来源:发表于2017-08-02 09:58 被阅读243次
    1. 工程目录下创建目录: fixtures
    2. settings.py 增加设置: FIXTURE_DIRS = (os.path.join(BASE_DIR,'fixtures',),)
    3. 编写初始化数据文件: initial_data 后缀可以根据自己的为.json .yaml 等
      注意:django自带user的model为:auth.User, 不要写成django.contrib.auto.User
    # initial_data.yaml
    # initial date for vidsai pin
    - model: auth.User
      pk: 1
      fields:
        username: abeladmin
        email: 972237007@qq.com
        password: abeladmin
        is_superuser: 1
        is_staff: 1
    
    - model: auth.User
      pk: 2
      fields:
        username: abeltest
        email: 972237007@qq.com
        password: abeltest
        is_superuser: 0
        is_staff: 0
    
    - model: upload.Album
      pk: 1
      fields:
        album_name: abeltest
        user_id: 2
        album_status: 1
        create_time: 2017-01-01 20:00:00
        update_time: 2017-01-01 20:00:00
    
    - model: upload.Media
      pk: 1
      fields:
        album_id: 1
        user_id: 2
        media_name: AbelTestJpg
        file: pin/1.jpg
        media_type: 1
        media_status: 1
        create_time: 2017-01-01 20:00:00
    
    - model: upload.Media
      pk: 2
      fields:
        album_id: 1
        user_id: 2
        media_name: AbelTestMp4
        file: pin/1.mp4
        media_type: 2
        media_status: 1
        create_time: 2017-01-01 20:00:00
    
    1. 执行:python manage.py loaddata initial_data.yaml

    相关文章

      网友评论

          本文标题:django初始化数据yaml

          本文链接:https://www.haomeiwen.com/subject/ksrelxtx.html