django orm JSONField for mysql - python

I am trying to define a model with one column being a mysql json type. Searching on web results in suggestions related to django's postgresql support for JSONField type or django-jsonfield.
Is there currently no way django natively supports mysql's json type. Also while doing inspectdb a column of type json in mysql was assigned the type TextField with a comment This field type is a guess.
How would I declare my model in such a way that it supports json fields?

UPDATE
As of Django 3.2, it now supports JSONField and django-mysql JSONField is now deprecated.
Check out django-mysql.
Django-MySQL supports the JSON data type and related functions through JSONField plus some JSON database functions.

You've already found django-jsonfield, but here are some recent discussions you may be interested in:
https://groups.google.com/forum/#!searchin/django-developers/mysql$20json%7Csort:relevance/django-developers/zfred27yVPg/U-4iNd_aAQAJ
https://groups.google.com/forum/#!searchin/django-developers/mysql$20json%7Csort:relevance/django-developers/sAgYOqBUvgI/aLQH6vx5CAAJ
AFAIK there isn't anything native currently, but people seem interested in developing what you're looking for.

Related

Get column values from MongoDB table to Django

How should I create a model.py, when I already have an existing mongoDB database.
I only want to get the values from an existing Table to a the HttpResponse in Django views.py.
Is there any reference material for me to go over this?
Django internally doesn't support mongo directly and you should handle your tasks manually. (fetch data and render it yourself)
I recommend checking mongoengine which provides a django-orm like api for mongo which can be nice.
If you don't like mongoengine you can always use pymongo, the official python module for mongo

Django hstore field in sqlite

I am using sqlite (development stage) database for my django project. I would like to store a dictionary field in a model. In this respect, i would like to use django-hstore field in my model.
My question is, can i use django-hstore dictionary field in my model even though i am using sqlite as my database?
As per my understanding django-hstore can be used along with PostgreSQL (Correct me if i am wrong). Any suggestion in the right direction is highly appreciated. Thank you.
hstore is specific to Postgres. It won't work on sqlite.
If you just want to store JSON, and don't need to search within it, then you can use one of the many third-party JSONField implementations.

why "models.py" is not required in django when using mongodb as backend?

Recetly I've seen an app powered with django and mongodb as backend,thing is that app doesn't have a models.py file.All the datas are inserted directly in views.py.I Just need a little clarification about this particular things "Using django without models.py with mongodb."
models.py is the Django ORM way of inspecting a fixed relational schema and generating the relevant SQL code to initialize (or modify) the database. "ORM" stands for "Object-Relational Mapping".
Mongo is not relational, hence you don't need this type of schema.
(Of course, that can cause a lot of other problems if the needs of your project change later...)
But you don't need a relational schema since you're not using a relational DB.
A short answer
models.py is the ORM that comes free with django.
ORM relates your SQL schema into oopsy objects.
You can read more about ORM here-> https://en.wikipedia.org/wiki/Object-relational_mapping.
When using a noSQL, you can push objects directly into DB. So, you do not really need an ORM.
That said, whether to use it or not is a debatable part.
PS. even while using SQL, some people prefer other ORMs instead of django's built-in models.

Model field types for Django 1.8.2 models that will work with MySQL database tables

Let's say I have the following table showing how the columns would be declared for a MySQL table: (I can't think of a very realistic example, so here's something that's so silly. This table is created with the help of Excel)
I want to create a model in Django that is compatible with the MySQL table I'll have with the columns declared this way. However, from looking at the Django documentation, I can't find any model field types that in SQL are the same format as those in the picture except for the primary key field.
I did see before that by default, Django handles a database that uses the SQLite Engine, but I want to see if it's possible to handle a database of MySQL tables.
Is there a way to create Django model field types, like MEDIUMINT, TINYTEXT, and SMALLINT, (in MySQL) that are compatible with tables created through MySQL? It's simply a way for me to use the tables that I created myself, not the tables that Django generates automatically once all the models are defined.
In Django you normally create the models and let the framework generate the tables for you. If you have a legacy database you can use python manage.py inspectdb to generate the models from the database (see the documentation). But if it isn't a legacy database and you've created it just now, you are fighting against the framework and making your life more complicated.

sorting by a related item with ming on mongodb

setup
A TurboGears2 project using ming as an ORM for mongodb. I'm used to working with relational databases and the Django ORM.
question
Ming claims to let to interact with mongodb like it's a relational database and a common thing to do in a relational database is sort a query by a property of a foreign key. With the Django ORM this is expressed with double underscores like so: MyModel.objects.all().order_by('user__username')
Is there an equivalent for this in ming?
I have never used ming, but they seem to have a sort method that you can add to a query, check it out here, not much in the form of documentation
I use mongoengine, it has great documentation and its very similar to the django ORM

Categories