วันเสาร์ที่ 15 มิถุนายน พ.ศ. 2556

Custom Field in Django with South Support

To define custom field in Django, here is an example:
detail here >> http://djangosnippets.org/snippets/2513/ and http://djangosnippets.org/snippets/1244/

class TinyIntegerField(models.SmallIntegerField):
    def db_type(self, connection):
        if connection.settings_dict['ENGINE'] == 'django.db.backends.mysql':
            return "tinyint"
        else:
            return super(TinyIntegerField, self).db_type(connection)

    def get_internal_type(self):
        return "TinyIntegerField"

in this example, db_type() tell Django to use field type tinyint if backend is MySQL

Using this custom field type, when try to migrate with South, this error happened.
 ! South cannot introspect some fields; this is probably because they are custom
 ! fields. If they worked in 0.6 or below, this is because we have removed the
 ! models parser (it often broke things).
 ! To fix this, read http://south.aeracode.org/wiki/MyFieldsDontWork

To fix this problem, add a method south_field_triple in your custom field class
detail here >> http://south.readthedocs.org/en/latest/customfields.html

    def south_field_triple(self):
        from south.modelsinspector import introspector
        field_class = self.__class__.__module__ + '.' + self.__class__.__name__
        args, kwargs = introspector(self)
        return (field_class, args, kwargs)

ไม่มีความคิดเห็น:

แสดงความคิดเห็น