berinhard/model_mommy

Error when using the JSONField()

elcolie opened this issue · 3 comments

class OrderItem(models.Model):
    booking = models.ForeignKey(FactoryBooking, related_name='order_items')
    order_item_data = JSONField(blank=True, verbose_name="order item data"))

mommy.make(OrderItem)

IntegrityError: null value in column "order_item_data" violates not-null constraint
DETAIL:  Failing row contains (9, null, 0, 1.00, 0, null, 369).

I have the existing data and then try mommy.make

Am I miss something Django 1.11
Python 3.6.0

thanks for the report @elcolie

Ok, digging in a bit further i found this is Django's default behaviour.

JSONField does not behaves like Char and Text fields, where they set a blank string '' by the time they are instantiated. Some good ways to handle that idiosyncrasy:
mommy.make(OrderItem, _fill_optional=['order_item_data'])
mommy.make(OrderItem, _fill_optional=True) -> this one would fill all blanks and nulls
mommy.make(OrderItem, order_item_data='')

If that becomes too cumbersome, you can always rely on recipes: https://model-mommy.readthedocs.io/en/latest/recipes.html

Thank you for your response. Very informative. If I have any question I will get back to you again.