Also irrespective of whether the DB supports JSON datatype or not, JSON
can be implemented by modifying TextField. And it believe it is very
helpful. Why don't we simply provide it? Many programmers I know, do their
custom implementation of JSONField on almost all their projects.
Something like this,
{{{
class JSONField(models.TextField):
""" JSON field implementation on top of django textfield """
def to_dict(self, value):
""" convert json string to python dictionary """
return json.loads(value)
def to_json(self, value):
""" convert python dictionary to json string """
return json.dumps(value)
def from_db_value(self, value, expression, connection):
""" convert string from db to python dictionary """
if value is None:
return value
return self.to_dict(value)
def to_python(self, value):
""" convert model input value to python dictionary """
if isinstance(value, dict):
return value
if value is None:
return value
return self.to_dict(value)
def get_prep_value(self, value):
""" convert python dictionary to string before writing to db """
return self.to_json(value)
}}}
Could you guys please clarify Django teams perspective on this.
--
Ticket URL: <https://code.djangoproject.com/ticket/29077>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
Comment:
Adding a database-agnostic JSONField was
[https://groups.google.com/d/topic/django-
developers/zfred27yVPg/discussion discussed on the mailing list]. Absent
that happening, [https://pypi.python.org/pypi/django-mysql django-mysql]
provides a `JSONField` for MySQL.
In the future, please see TicketClosingReasons/UseSupportChannels for ways
to find answers to "help" questions.
--
Ticket URL: <https://code.djangoproject.com/ticket/29077#comment:1>
Comment (by Carlton Gibson):
Proposal for cross DB JSONField #29821
--
Ticket URL: <https://code.djangoproject.com/ticket/29077#comment:2>