django-model-utils/tests/fields.py
Misha K 5aa40c66be Add django 3.0 to the test matrix and drop six (#395)
*  - add django 3.0 to the test matrix
 - drop six

* add entry in CHANGES

* remove context kwarg

* fix test with DeferredAttribute

* rename StringyDescriptor's name to attname

* Fix flake8

* Drop support for Django 1.11 because the API are not compatibles anymore with Django 3.0

* Try to fix tests.

* Define model for the field mock.

* Simplifies the code.

* Properly mock the field.

* Typo

* Use the new API field name.

* Call it attname

* Grab the field instance from the model.

* Use postgres in travis tests.

* Django 2.0.1 minimum is needed.

* Update Changelog to tell about breaking Django 1.11.

* Update changelog to tell about Django 3.0 support.

* @natim review.
2019-12-09 19:37:16 +06:00

32 lines
780 B
Python

from django.db import models
def mutable_from_db(value):
if value == '':
return None
try:
if isinstance(value, (str,)):
return [int(i) for i in value.split(',')]
except ValueError:
pass
return value
def mutable_to_db(value):
if value is None:
return ''
if isinstance(value, list):
value = ','.join(str(i) for i in value)
return str(value)
class MutableField(models.TextField):
def to_python(self, value):
return mutable_from_db(value)
def from_db_value(self, value, expression, connection):
return mutable_from_db(value)
def get_db_prep_save(self, value, connection):
value = super().get_db_prep_save(value, connection)
return mutable_to_db(value)