zsiciarz / django-markitup

Markup handling (using Jay Salvat's MarkItUp! flexible universal markup editor) for Django
https://github.com/zsiciarz/django-markitup
BSD 3-Clause "New" or "Revised" License
47 stars 31 forks source link

`value` in `pre_save` and `value_to_string` is str using factory-boy #20

Open yofe opened 7 years ago

yofe commented 7 years ago

I get this error when I want to test my code using factory-boy:

AttributeError: 'str' object has no attribute 'raw'. See also this question on stackoverflow.

Therefore I have to subclass the MarkupField:

from markitup.fields import render_func, _rendered_field_name

class MyMarkupField(MarkupField):
    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        if hasattr(value, "raw"):
            return value.raw
        return value

    def pre_save(self, model_instance, add):
        value = super(MarkupField, self).pre_save(model_instance, add)
        if hasattr(value, "raw"):
            value = value.raw
        elif value is None:
            value = ''
        rendered = render_func(value)
        setattr(model_instance, _rendered_field_name(self.attname), rendered)
        return value

This isn't ideal. Can we fix this, please?