Search code examples
pythondjangofactory-boy

cannot create from a factoryBoy class


I try to make a class a make some instances but I can't create any instance with the class in my django shell

factories.py

import factory
from webshare.models import WebShareFileFolders


class WebShareFileFactoryBoy(factory.django.DjangoModelFactory):
    class Meta:
        model = WebShareFileFolders

    inode = factory.Sequence(lambda n: n)
    name = factory.Faker('name')
    path = factory.Faker('text', max_nb_chars=50)
    is_dir = factory.Faker('boolean')

and then the error in my console

>>> from webshare.tests.factories import WebShareFileFactoryBoy

>>> instance = WebShareFileFactoryBoy()
Traceback (most recent call last):
  File "C:\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: 'module' object is not callable

>>> instance = WebShareFileFactoryBoy.create()
Traceback (most recent call last):
  File "C:\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
AttributeError: module 'webshare.tests.factories.WebShareFileFactoryBoy' has no attribute 'create'

Thank you for your help ;)

Python 3.7.9 factory-boy 3.3.0

I just try to make instance from a factory class

edit : the solution is the import method

from webshare.tests.factories.WebShareFileFactoryBoy import WebShareFileFactoryBoy factories is the directory and the python file have the same name than the class


Solution

  • I made a mistake when I import the class

    the problem: from webshare.tests.factories import WebShareFileFactoryBoy

    the solution: from webshare.tests.factories.WebShareFileFactoryBoy import WebShareFileFactoryBoy

    factories is the directory and the python file have the same name than the class

    second solution: in the init.py add

    from .WebShareFileFactoryBoy import WebShareFileFactoryBoy