django load data命令用来加载auth用户

时间:2014-06-05 08:50:06

标签: django django-models

INSERT INTO `auth_group` VALUES (4,'ascs'),(3,'customer'),(1,'dealers'),(2,'sas');

此命令在auth_group表上创建组。但是,就负载数据而言,我需要在django中使用类似的命令。

2 个答案:

答案 0 :(得分:2)

您可以在数据库中创建fixture and load it

[
  {
    "model": "auth.group",
    "pk": 4,
    "fields": {
      "name": "ascs",
    }
  },
  {
    "model": "auth.group",
    "pk": 3,
    "fields": {
      "name": "customer",
    }
  },
  # ... you get the idea
]

然后django-admin.py loaddata <fixture_file>See more about loaddata also。正如文档所说,现在避免自动装夹。您应该使用迁移:

  

自动加载初始数据夹具

     

在Django 1.7中弃用:

     

从1.7版开始不推荐使用:如果应用程序使用迁移,则不会自动加载灯具。由于Django 2.0中的应用程序将需要迁移,因此不推荐使用此行为。如果要加载应用程序的初始数据,请考虑在迁移中执行此操作。

答案 1 :(得分:0)

我们可以使用自定义加载命令添加迁移数据。

而不是loaddata 我创建了自己的自定义命令,名称为load_migration_data

from django.core.management.base import BaseCommand
from django.conf import  settings

class Command(BaseCommand):

    def handle(self, *args, **options):
        self.add_group()

了解更多信息 https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

相关问题