RemovedInDjango110Warning:已弃用SubfieldBase

时间:2016-03-10 05:57:59

标签: python django

在fields.py文件中进行更改时,我没有收到任何警告,但应用程序无效。

DIR:/home/ammy/aamp/lib/python2.7/site-packages/geoposition/fields.py

代码段:

from __future__ import unicode_literals

from django.db import models
from django.utils.six import with_metaclass
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_text

from . import Geoposition
from .forms import GeopositionField as GeopositionFormField


class GeopositionField(with_metaclass(models.SubfieldBase, models.Field)):
    description = _("A geoposition (latitude and longitude)")

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 42
        super(GeopositionField, self).__init__(*args, **kwargs)

    def get_internal_type(self):
        return 'CharField'

    def to_python(self, value):
        if not value or value == 'None':
            return None
        if isinstance(value, Geoposition):
            return value
        if isinstance(value, list):
            return Geoposition(value[0], value[1])

        # default case is string
        value_parts = value.rsplit(',')
        try:
            latitude = value_parts[0]
        except IndexError:
            latitude = '0.0'
        try:
            longitude = value_parts[1]
        except IndexError:
            longitude = '0.0'

        return Geoposition(latitude, longitude)

    def get_prep_value(self, value):
        return str(value)

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return smart_text(value)

    def formfield(self, **kwargs):
        defaults = {
            'form_class': GeopositionFormField
        }
        defaults.update(kwargs)
        return super(GeopositionField, self).formfield(**defaults)

错误:

  

RemovedInDjango110Warning:已弃用SubfieldBase。使用   而是改为Field.from_db_value。 return meta(name,bases,d)

1 个答案:

答案 0 :(得分:2)

问题出在这一行:

class GeopositionField(with_metaclass(models.SubfieldBase, models.Field)):

将其更改为:

class GeopositionField(models.Field):

您还必须实现from_db_value(),如下所述: https://docs.djangoproject.com/en/1.9/howto/custom-model-fields/#converting-values-to-python-objects