mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-17 12:20:23 +00:00
* - 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.
32 lines
780 B
Python
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)
|