Have a way on a Factory to configure whether a field is generated for that overrides blank/null
Bug #915418 reported by
James Westby
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Django Factory |
New
|
Undecided
|
Unassigned |
Bug Description
As sometimes people want this but don't want to change blank/null.
To post a comment you must log in.
I'm not sure if this is the same thing, but for models with nullable foreign keys, I find my self adding custom factories like this to the model:
{{{ models. Model): ForeignKey( 'categories. Category' , null=True, blank=True)
class Transaction(
...
category = models.
class Factory:
@staticmethod prepare_ one(Category)
def get_category(field, factory):
return factory.
}}}
This is because, mostly if I have a nullable field, it's usually because the data is not yet available, but it's always intended to be filled. There fore the majority of my tests want every Transaction with a category, and overriding this for the few tests that don't is already handled out-of-the-box by django-factor with `factory. make_one( Transaction, category=None)`
This is not the case the other way around - if the default is to create Transactions with a null category, then all those tests requiring a category need quite a bit more code: `factory. make_one( Transaction, category= factory. make_one( Category) )'.
I'm not sure yet if it's fair to say that it should be the default to populate nullable FKs - but it seems that a value of null on a nullable FK is the exception, rather than the rule - not sure.