Hello,
I'm encountering an issue when trying to create a Factory using OdmanticModelFactory. If the Odmantic model contains datetime attributes, the following error is raised:
AttributeError: type object '_datetime' has no attribute 'validate'
The error originates from line 48 in the file odmantic_odm_factory.py.
Could you please provide guidance on resolving this issue? Thank you!
No response
class Test(Model):
date: datetime = datetime.now()
name: str = "Jean"
class TestFactory( OdmanticModelFactory[Test]): ...
TestFactory.build()
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
"In the format of: 
"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[438], line 7
3 name: str = "Jean"
5 class TestFactory( OdmanticModelFactory[Test]): ...
----> 7 TestFactory.build()
File ~/opt/miniconda3/envs/AdaptPA/lib/python3.10/site-packages/polyfactory/factories/pydantic_factory.py:473, in ModelFactory.build(cls, factory_use_construct, **kwargs)
468 if "_build_context" not in kwargs:
469 kwargs["_build_context"] = PydanticBuildContext(
470 seen_models=set(), factory_use_construct=factory_use_construct
471 )
--> 473 processed_kwargs = cls.process_kwargs(**kwargs)
475 return cls._create_model(kwargs["_build_context"], **processed_kwargs)
File ~/opt/miniconda3/envs/AdaptPA/lib/python3.10/site-packages/polyfactory/factories/base.py:1003, in BaseFactory.process_kwargs(cls, **kwargs)
996 result[field_meta.name] = cls._handle_factory_field(
997 field_value=field_value,
998 field_build_parameters=field_build_parameters,
999 build_context=_build_context,
1000 )
1001 continue
-> 1003 field_result = cls.get_field_value(
1004 field_meta,
1005 field_build_parameters=field_build_parameters,
1006 build_context=_build_context,
1007 )
1008 if field_result is Null:
1009 continue
File ~/opt/miniconda3/envs/AdaptPA/lib/python3.10/site-packages/polyfactory/factories/base.py:785, in BaseFactory.get_field_value(cls, field_meta, field_build_parameters, build_context)
776 return handle_collection_type(
777 field_meta,
778 origin,
(...)
781 build_context=build_context,
782 )
784 if provider := cls.get_provider_map().get(unwrapped_annotation):
--> 785 return provider()
787 if isinstance(unwrapped_annotation, TypeVar):
788 return create_random_string(cls.__random__, min_length=1, max_length=10)
File ~/opt/miniconda3/envs/AdaptPA/lib/python3.10/site-packages/polyfactory/factories/odmantic_odm_factory.py:48, in OdmanticModelFactory.get_provider_map.<locals>.<lambda>()
40 @classmethod
41 def get_provider_map(cls) -> dict[Any, Callable[[], Any]]:
42 provider_map = super().get_provider_map()
43 provider_map.update(
44 {
45 odbson.Int64: lambda: odbson.Int64.validate(cls.__faker__.pyint()),
46 odbson.Decimal128: lambda: _to_decimal128(cls.__faker__.pydecimal()),
47 odbson.Binary: lambda: odbson.Binary.validate(create_random_bytes(cls.__random__)),
---> 48 odbson._datetime: lambda: odbson._datetime.validate(cls.__faker__.date_time_between()),
49 # bson.Regex and bson._Pattern not supported as there is no way to generate
50 # a random regular expression with Faker
51 # bson.Regex:
52 # bson._Pattern:
53 },
54 )
55 return provider_map
AttributeError: type object '_datetime' has no attribute 'validate'
2.18.1
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too