Have a way on a Factory to configure whether a field is generated for that overrides blank/null

Bug #915418 reported by James Westby
8
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.

Revision history for this message
Michael Nelson (michael.nelson) wrote :

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:

{{{
class Transaction(models.Model):
    ...
    category = models.ForeignKey('categories.Category', null=True, blank=True)

    class Factory:
        @staticmethod
        def get_category(field, factory):
            return factory.prepare_one(Category)
}}}

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.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.