Right way add dynamically fields to model and migrations

58 views
Skip to first unread message

Alexandr Shurigin

unread,
Jun 28, 2014, 5:52:58 PM6/28/14
to django...@googlegroups.com
Hi all!

Have little problem.

I have Custom Field like abstract RichTextEditor which can parse entered text to html some ways. For performance optimization this fields caches parsed data in separate field of model which creates dynamically.

Take a look into code.

    def contribute_to_class(self, cls, name):
        if self.add_rendered_field and not cls._meta.abstract:
            preview_field = models.TextField(editable=False, blank=True, null='')
            cls.add_to_class(_preview_field_name(name), preview_field)

            readmore_field = models.CharField(editable=False, blank=True, null='', max_length=250)
            cls.add_to_class(_readmore_field_name(name), readmore_field)
        super(RichTextField, self).contribute_to_class(cls, name)
        setattr(cls, self.name, MarkupDescriptor(self))

For example in my model i adding this type of fields by piece of code:

content = RichTextEditor()

All working good until i want to migrate this app :)

As result of migration i getting fields in migration file.

                ('content', project.apps.utils.fields.RichTextField()),
                ('_content_preview', models.TextField(null=b'', editable=False, blank=True)),
                ('_content_readmore', models.CharField(max_length=250, null=b'', editable=False, blank=True)),

This is ok, but when i start migration i getting error.

django.db.utils.ProgrammingError: column "_content_preview" specified more than once.

This error throwed because one time this field added from migration file and second time it added from field initialization for content field.

                ('content', project.apps.utils.fields.RichTextField()),


Does anybody have same problems? Thanks, alex.
Reply all
Reply to author
Forward
0 new messages