Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29077 closed Uncategorized (invalid)

[Help] Why JSONField for MySql/MariaDB etc are not supported in Django?

Reported by: Akhil Lawrence Owned by: nobody
Component: Uncategorized Version: 2.0
Severity: Normal Keywords: help
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

JSON format is widely used these days. Databases like [MariaDB](https://mariadb.com/kb/en/library/json-data-type/), [MySql](https://dev.mysql.com/doc/refman/5.7/en/json.html) etc supports JSONField. But within Django, JSONField is only provided for the postgres. Why is it so? Is there any reason for this?

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.

Change History (2)

comment:1 by Tim Graham, 6 years ago

Resolution: invalid
Status: newclosed

Adding a database-agnostic JSONField was discussed on the mailing list. Absent that happening, django-mysql provides a JSONField for MySQL.

In the future, please see TicketClosingReasons/UseSupportChannels for ways to find answers to "help" questions.

comment:2 by Carlton Gibson, 6 years ago

Proposal for cross DB JSONField #29821

Note: See TracTickets for help on using tickets.
Back to Top