17๐
You can create dict data using a factory.Dict
. You can then control the final form of the data using the dict_factory
attribute.
e.g. if you want to have dict
data that is serialised to a JSON string, you can do something like this:
import json
import factory
class JSONFactory(factory.DictFactory):
"""
Use with factory.Dict to make JSON strings.
"""
@classmethod
def _generate(cls, create, attrs):
obj = super()._generate(create, attrs)
return json.dumps(obj)
class BadgerFactory(factory.Factory):
form_data = factory.Dict({
'badger': ['stoat'],
}, dict_factory=JSONFactory)
class Meta:
model = ...
๐คDanielle Madeley
-1๐
Have you tried passing the value as a dictionary? It must be then converted to JSON.
๐คsilentser
- Receiving 'invalid form: crispy' error when trying to use crispy forms filter on a form in Django, but only in one django app and not the other?
- My first web app (Python): use CGI, or a framework like Django?
Source:stackexchange.com