填充Django数据库 [英] Populate Django database

查看:118
本文介绍了填充Django数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Django应用程序,该应用程序存储用户信息,例如其地址,电话号码,姓名等.

I'm developing a Django application which stores user information like their address, phone number, name, etc.

我使用过PHP的 Faker 库以及Laravel附带的种子.我本来可以用假数据填充数据库,但是现在我正在使用Django.

I've worked with PHP's Faker library and the seeder included with Laravel. I had been able to populate the database with fake data but now I'm working with Django.

我想在我的users表中填充大约200个条目.但是,我不希望这些条目是随机字符串.我希望它是像Laravel一样的伪造数据.我不知道该怎么办.

I'd like to populate my users table with around 200 entries. But, I don't want the entries to be random strings. I want it to be fake data like I can get with Laravel. I don't know how to do it.

要保留伪造数据我需要怎么做?

What do I need to do to persist fake data?

这是为了向最终用户显示带有一些条目的应用程序,以便他可以查看统计信息和其他内容.数据将需要保留在数据库中.我尝试使用单元测试,但是在单元测试结束后会删除数据库.

This is for showing the end user the application with some entries so he can see statistics and other things. The data will need to stay in the database. I tried using unit tests but that deletes the database after the unit test ends.

谢谢!

推荐答案

要以一种不错的方式完成它,您需要结合使用工厂男孩 Faker

To get it done in a nice way you'll need a combination of Factory Boy, Faker and custom management commands.

Factory Boy允许您创建用于生成有效对象的模板,而Faker可以生成伪造数据.

Factory Boy allows you to create templates for producing valid objects and Faker generates fake data.

在安装Factory Boy pip install factory_boy时,您还将获得Faker.

When you install Factory Boy, pip install factory_boy, you also get Faker.

给出

from django.db import models


class User(models.Model):
    name = models.CharField(max_length=64)
    address = models.CharField(max_length=128)
    phone_number = models.CharField(max_length=32)

您可以如下定义工厂:

import factory  
import factory.django

class UserFactory(factory.django.DjangoModelFactory):  
    class Meta:
        model = User

    name = factory.Faker('name')
    address = factory.Faker('address')
    phone_number = factory.Faker('phone_number')

然后,您可以通过调用UserFactory.create()来创建假用户.

Then, you can create fake users by calling UserFactory.create().

获取200个假用户的一种方法是跳入外壳python manage.py shell,然后执行以下操作:

One way to get your 200 fake users would be to jump into the shell, python manage.py shell, and do:

 >>> # import UserFactory here
 >>> for _ in range(200):
 ...     UserFactory.create()

另一种可以为您提供更大灵活性的方法是创建自定义管理命令.

Another way, which can give you a lot more flexibility, is to create a custom management command.

例如,使用以下命令在目录<yourapp>/management/commands(由Django发现)中创建seed.py(这将是管理命令名称):

For example, create seed.py (this will be the management command name) in the directory <yourapp>/management/commands (to have it discovered by Django) with the following:

# <yourapp>/management/commands/seed.py
from django.core.management.base import BaseCommand

# import UserFactory here


class Command(BaseCommand):
    help = 'Seeds the database.'

    def add_arguments(self, parser):
        parser.add_argument('--users',
            default=200,
            type=int,
            help='The number of fake users to create.')

    def handle(self, *args, **options):
        for _ in range(options['users']):
            UserFactory.create()

而且,您可以通过命令行使用python manage.py seedpython manage.py seed --users 50来运行它.

And, you'd run it via the command-line with python manage.py seed or python manage.py seed --users 50 for example.

这篇关于填充Django数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆