factory_boy 默认1
用于序列。如何传递一个数字以用作不同的起始数字?我可以对_setup_next_sequence()
方法进行子类化,但是如何给它一个变量来使用?
# File: models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
# File: factories.py
from .models import Book
import factory
class BookFactory(factory.Factory):
FACTORY_FOR = BookModel
title = factory.Sequence(lambda n: u'Title #{}'.format(n))
@classmethod
def _setup_next_sequence(cls):
# Instead of defaulting to starting with number 1, start with starting_seq_num.
# But how do I set starting_seq_num?
return starting_seq_num
# File: make_data.py
from factories import BookFactory
# somehow set starting sequence number here?
BookFactory().create()
我正在使用 factory_boy 1.2.0(通过pip install factory_boy
)
factory_boy 代码:https ://github.com/dnerdy/factory_boy