翻译进度
3
分块数量
0
参与人数

数据库填充

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。


Seeding

Seeding is simply a way to quickly seed, or put data into your tables.

Creating Seeds

You can create a seed file and seed class which can be used for keeping seed information and running it later.

To create a seed run the command:

$ masonite-orm seed User

This will create some boiler plate for your seeds that look like this:

from masoniteorm.seeds import Seeder

class UserTableSeeder(Seeder):

    def run(self):
        """Run the database seeds."""
        pass

From here you can start building your seed.

Building Your Seed

A simple seed might be creating a specific user that you use during testing.

from masoniteorm.seeds import Seeder
from models import User

class UserTableSeeder(Seeder):

    def run(self):
        """Run the database seeds."""
        User.create({
            "username": "Joe",
            "email": "joe@masoniteproject.com",
            "password": "secret"
        })

Running Seeds

You can easily run your seeds:

$ masonite-orm seed:run User

Database Seeder

Factories

Factories are simple and easy ways to generate mass amounts of data quickly. You can put all your factories into a single file.

Creating A Factory Method

Factory methods are simple methods that take a single Faker instance.

# config/factories.py

def user_factory(faker):
    return {
        'name': faker.name(),
        'email': faker.email(),
        'password': 'secret'
    }

For methods available on the faker variable reference the Faker documentation.

Registering Factories

Once created you can register the method with the Factory class:

# config/factories.py
from masoniteorm import Factory
from models import User

def user_factory(faker):
    return {
        'name': faker.name(),
        'email': faker.email(),
        'password': 'secret'
    }

Factory.register(User, user_factory)

Naming Factories

If you need to you can also name your factories so you can use different factories for different use cases:

# config/factories.py
from masoniteorm import Factory
from models import User

def user_factory(faker):
    return {
        'name': faker.name(),
        'email': faker.email(),
        'password': 'secret'
    }

def admin_user_factory(faker):
    return {
        'name': faker.name(),
        'email': faker.email(),
        'password': 'secret',
        'is_admin': 1
    }

Factory.register(User, user_factory)
Factory.register(User, admin_user_factory, name="admin_users")

Calling Factories

To use the factories you can import the Factory class from where you built your factories. In our case it was the config/factories.py file:

from config.factories import Factory
from models import User

users = Factory(User, 50).create() #== <masoniteorm.collections.Collection object>
user = Factory(User).create() #== <models.User object>

This will persist these users to the database. If you want to simply make the models or collection (and not persist them) then use the make method:

from config.factories import Factory
from models import User

users = Factory(User, 50).make() #== <masoniteorm.collections.Collection object>
user = Factory(User).make() #== <models.User object>

Again this will NOT persist values to the database.

Calling Named Factories

By default, Masonite will use the factory you created without a name. If you named the factories you can call those specific factories easily:

from config.factories import Factory
from models import User

users = Factory(User, 50).create(name="admin_users") #== <masoniteorm.collections.Collection object>

After Creating

You can also specify a second factory method that will run after a model is created. This would look like:

# config/factories.py
from masoniteorm import Factory
from models import User

def user_factory(faker):
    return {
        'name': faker.name(),
        'email': faker.email(),
        'password': 'secret'
    }

def after_users(model, faker):
    model.verified = True

Factory.register(User, user_factory)
Factory.after_creating(User, after_users)

Now when you create a user it will be passed to this after_creating method:

user = factory(User).create()
user.verified #== True

Modifying Factory Values

If you want to modify any values you previously set in the factory you created, you can pass a dictionary into the create or make method:

from config.factories import Factory
from models import User

users = Factory(User, 50).create({'email': 'john@masoniteproject.com'}) #== <masoniteorm.collections.Collection object>

This is a great way to make constant values when testing that you can later assert to.

本文章首发在 LearnKu.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

讨论数量: 0
发起讨论 只看当前版本


暂无话题~