FactoryBoy / factory_boy

A test fixtures replacement for Python
https://factoryboy.readthedocs.io/
MIT License
3.48k stars 392 forks source link

[NEW FEATURE SUGGESTION] Provide factory_boy defaults for common Django model attributes #347

Open YPCrumble opened 7 years ago

YPCrumble commented 7 years ago

Thank you for this great library!

This is similar to #330 but I would like to extend it to include a default factory.Fuzzy value for common fields like CharField, IntegerField, etc. that are not blank/nullable in the db.

Right now for a model like this:

class MyModel(Model):
    field1 = CharField(max_length=75)
    field2 = CharField(max_length=75, blank=True)

...I have to specify a field value explicitly using factory_boy (let me know if this isn't the case and I'm missing something!):

class MyModelFactory(DjangoModelFactoryWithDefaults):
    class Meta:
        model = MyModel
    field1 = factory.fuzzy.FuzzyText() # I'd like this to just be a default for any CharField without `blank=True`

...for large models this would save a lot of typing.

I apologize in advance if this is already possible and I missed it or if it's been brought up before - I searched open and closed issues and couldn't find anything. I'd also love to hear if there's a reason it's just way to complicated to achieve!

rbarrois commented 7 years ago

Indeed; this is the goal of the (stalled) https://github.com/FactoryBoy/factory_boy/tree/wip/auto_factory branch.

I might be able to give it a shot at a future open-source sprint I would be attending. Once the core is done, it would be easier to auto-define default values.

YPCrumble commented 7 years ago

@rbarrois very cool! Let me know if I could be helpful in any way? Happy to contribute.

stephenross commented 6 years ago

With Faker being part of factory_boy now for random generation, it might be useful as mentioned in a comment on another issue, to get a PR going for Faker to have max number of characters on more of the providers, and then simply hook into the pre-defined providers that exist there for the automation.

kdickerson commented 5 years ago

I've implemented this functionality in the form of a custom FactoryOptions class (overriding declarations seemed to be the only place I could effectively hook in without forking the code). It will automatically wire up a Faker Provider based on the name/type of the field.

However, I need to work with my employer before I can push it out to the project. Hopefully I can polish the code and get it pushed out within a few months. Lots of higher priorities on my plate at the moment.