Dynamic model fields using mysql?

78 views
Skip to first unread message

hardik dadhich

unread,
Jul 2, 2018, 3:48:46 PM7/2/18
to Django users
Hello,
Can anyone tell me how can I make dynamic Django model fields? I mean to say that I want to save excel file data in Django model using MySQL. and excel column can vary. The user can upload 100 column table or he can be upload 1000 column table in the database. so it is possible to add dynamically in db. 

Any help would be appreciated.
Thanks in advance!



Melvyn Sopacua

unread,
Jul 2, 2018, 5:50:40 PM7/2/18
to django...@googlegroups.com

On maandag 2 juli 2018 21:33:35 CEST hardik dadhich wrote:

 

> Can anyone tell me how can I make dynamic Django model fields? I mean to

> say that I want to save excel file data in Django model using MySQL. and

> excel column can vary. The user can upload 100 column table or he can be

> upload 1000 column table in the database. so it is possible to add

> dynamically in db.

 

It makes little sense to store this in a database, because you cannot define relations for what you don't know about. But if you must ...

 

One way to do this is to use the EAV data model. This is because at 1000 columns = 1000 fields, you will most certainly hit MySQL's maximum row size.

 

--

Melvyn Sopacua

Jason

unread,
Jul 2, 2018, 6:01:55 PM7/2/18
to Django users
or you can move to postgres and take advantage of the jsonfield integration with django.

hardik dadhich

unread,
Jul 2, 2018, 11:37:34 PM7/2/18
to Django users
it means that I can't do it on MySQL database! should I change the DB type and if so which one should I prefer! 
Postgres or NoSQL?

hanleybrand

unread,
Jul 3, 2018, 11:07:36 AM7/3/18
to Django users
One question I'd ask would be why you wouldn't just save the files either on disk or even in the database (using the  BLOB datatype) if it absolutely had to be in the database... then if you needed to manipulate/display/read them at a later date you would just load the files in question. 

If you really need to save the data in the database using a django model, take a look at MDID's solution in rooibos.data.models, especially the Record (which would be a row in the excel spreadsheet) and the Field (which would be a cell) models -- this isn't a copy/paste solution because the MDID app is more complicated than you want (it allows arbitrary metadata which may or may not be part of an established standard like Dublin Core, etc. to be stored as part of a record, and allows each field to be listed as an equivalent to another), but this implementation works with MySQL, and performance shouldn't be too bad, although if you wanted to implement good search capabilities you'll probably need to look at Solr or some other indexing solution. 

Good luck!


On Monday, July 2, 2018 at 3:48:46 PM UTC-4, hardik dadhich wrote:
Reply all
Reply to author
Forward
0 new messages