Django: How to use modelform on dynamic attributes..? - django

It's kind of tricky question, It's hard for me to expain what I want so please comment this post what should be edited. Thnx.
So there's a situation where there will be tons of dynamic form inputs. What do I mean by word "dynamic" is that - every attribute (form input) will be stored in table not as column but as value and name of form can be easily edited in the feature with out using migrations. Here's a scheme:
I want to have automatic modelform generated from this scheme. Django could just pick all field names/values/types from table1_attr table. And handle them as it's done with one-column-per-field scheme.
I know that it's possible to add any field later into the model, and then to use "south", but I really afraid of data migration with south. Because I had a lot of segfaults and errors before with this tool. Ofcourse this is an option, and if this scheme will fail - then I'll use usual solution. Btw middleware solutions also fine.

I think all you need is statically defined form in python code and NoSQL storage. As for Redis you can store data in hashes with numbers of keys.

Related

If I use django, I should not care about any MySQl optimization like indexation?

If I use django, I should not care about any MySQl optimization like indexation or something? Django automatically creates everything? Explain please
Answer: you should care, but not at first (usually).
Django doesn't take care of everything for you, only the basic/initial indexes (if you use migration tool and not creating the tables manually).
The index is related to the actual data that will be stored in the table, not only the table structure, somthing that is not known by django during the tables creation. Some indexs you get for "free", by creating unique constraints etc. (assuming you use tools like south that does that for you) but not real optimizations - this you'll have to do on your own when the time comes.
Some optimizations can be done by "telling" Django which indexes should be added, but you will have to specify it yourself.
No, Django only creates indices required by the database system, for example when creating foreign keys. You will have to optimise your database yourself, more on that in the documentation.
You can tell Django that certain model fields should be used as an index:
https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.Field.db_index
You can also index columns together:
https://docs.djangoproject.com/en/1.10/ref/models/options/#django.db.models.Options.index_together
Yes, you are right...with Django you can generate your database from your model (classes), you have to use specifics inheritance and types but it's pretty easy to handle and of course it's as optimal as could be needed.

What is the best way deal with large amount of fields in Django model?

Anyone can suggest me any good solution? The problem:
There is spreadsheets with data and i need to create a model with more than 40 of indexable fields (maybe even more in future) is there anything to help me to avoid hand work to define this fields manually? The fields are some kind of chemical parameters with long names, i guess need to create aliases to access them easily.
UPD: googled for a hour, can't find anything. Maybe i'll just create model dynamically from configuration stored in YAML or JSON file? Or use Postgres JSON field? But i feel it's gonna be disaster to make admin interface to work with it.
Well, after the accurate research of the subject i found few options:
Store main part of parameter in Postgres as a columns and the rest in mongodb and use proxy model/custom manager to retrieve this data when required.
Postgres HSTORE (but only for string fields) https://docs.djangoproject.com/en/1.9/ref/contrib/postgres/fields/#querying-hstorefield
And Entity Attribute Value Model https://en.wikipedia.org/wiki/Entity–attribute–value_model
Dynamically creating models from JSON/YAML configuration.

Why there isn't a "ListField" in Django?

I have a model that has a Charfield (let's name it advantages) with a choices attribute. After a while, I've decided that this field should be "upgraded" to some kind of ListField, since more than one choice can be selected.
From what I have searched, I have two options:
1 - Create a new model, and use a ManyToManyField in the first model referencing this new model. This way, the "multiple select" default field used in admin will be rendered. Life is good.
2- Create a custom field that saves my field as a string with some kind of separator.
These two approaches are summarized in What is the most efficent way to store a list in the Django models? and the 2nd approach in more examples: How to create list field in django, http://cramer.io/2008/08/08/custom-fields-in-django/, https://djangosnippets.org/snippets/1200/, https://djangosnippets.org/snippets/1491/
Fact is: I don't want to create another model just to have the ManyToManyField. This is a controlled list of choices that I have (and don't want people adding new items) and think that creating a table for this is overkill (although I can create a fixture to this table and not register the model in admin.py, so people wouldn't be adding new items. But I don't know how would migrations work when changing these values in fixtures, when in the past I would just chance the choices tuple in my model definition).
...and creating a new custom field, I don't know. This seems like problems in the long run since I don't know the implications, problems when upgrading Django, etc.
Why there isn't a built in ListField? Which problems do you see in the long run for the two approaches I'm thinking of? I'm planning to do the first but I'm a little lost about migrations.
django.contrib.postgres has an ArrayField.
You seem to not be willing to create a new table with only the List inside (because it would be "overkill"), but what you are suggesting is to copy/paste the same exact values in all the entries of your table, which isn't a good model solution in my opinion.
I'd advise to go for the ManyToMany (or any other implementation doing the trick with another table).

Django end-user defined fields, how to? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Django dynamic model fields
Good Morning guys!
Scenario is the following. For some models on Django, I would like to allow the end user to define his own fields. It would be great if I could keep all Django awesome features like the ORM, so I can still do calls like field__gte to search on the model, still have field validation according to field type, etc. I've thought about two ways of doing this, and I'm more than open for new suggestions. Any feedback would be VERY appreciated.
The first approach, is the Entity-Attribute-Value ( http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model ), which django already has an app for. See http://code.google.com/p/django-custom-field/
I think this would be an OK solution, but I lose the ability to do "mymodel.objects.filter(custom_field_x=something)". Maybe there's a way to regain the ORM, any ideas? But I've heard so many bad stories about this method that I'm little scared to use it.
The second approach would be to have a database table for each of the users (probably no more than a 1000). I've read django has something in the lines of inspectdb, which actually checks which fields are there and produces the model for you. This could be useful but I think maybe I should store the fields this particular user has created and somehow dinamically tell django, hey, we also have this fields in this model. Is this possible? I know it's generally bad to have different tables for each user, but considering this scenario, how would you guys rate this method, would it be ok to have one table for each user?
The model that requires custom fields is for example Person. They might want a custom field to store address, blood type, or any other thing.
MANY THANKS in advance! Have a nice sunday!
Very similar: How to create user defined fields in Django -- but only talks about the EAV, which I would like to avoid. I'm open for new ideas!
One approach is to use a NoSQL document-based solution such as MongoDB which allows you to store objects that have a fluid structure (no such restrictions as pre-defined columns).
Pros:
No restriction on custom field types, number of types of fields, etc.
Retains ORM functionality (django-mongodb)
Other various benefits of NoSQL - which you can read about online
Avoids EAV
Cons:
Need to setup NoSQL server
Additional knowledge required on NoSQL concepts (documents vs. tables)
You may have to maintain two databases - if you decide not to migrate your entire solution to NoSQL (multi-db)
EDIT:
After reading the comments its worth pointing out that depending on which NoSQL solution you go with, you may not need reversion support. CouchDB, for example has built in support for document versioning.
what about creating another model for storing user_defined_fields?
class UserDefinedField(models.Model):
#..................
user = models.ForeignKey(User)
field_name = models.CharField(max_length=50)
field_value = models.TextField()
Then you can do UserDefinedField.objects.filter(field_name=some_name,field_value=somevalue)

What do I use instead of ForiegnKey relationship for multi-db relation

I need to provide my users a list of choices from a model which is stored in a separate legacy database. Foreign keys aren't supported in django multi-db setups. The ideal choice would be to use a foreign key, but since thats not possible I need to some up with something else.
So instead I created an IntegerField on the other database and tried using choices to get a list of available options.
class OtherDBTable(models.Model):
client = models.IntegerField(max_length=20, choices=Client.objects.values_list('id','name'))
the problem I'm having is that the choices seem to get populated once but never refreshed. How do I ensure that whenever the client list changes that those newest options area available to pick.
What I was really looking for was a way that I could simulate the behavior of a Foreign key field, at least as far as matching up ID's go.
There wasn't a clear way to do this, since it doesn't seem like you can actually specify an additional field when you instantiate a model (you can with forms, easily)
In any case to deal with the problem, since the database is MySQL based, I ended up creating views from the tables I needed in the other database.
To build on #Yuji's answer - if you do self.fields['field'].choices = whatever in the model's __init__, whatever can be any iterable.
This means you can inherit from iterable, and have that object interface to your legacy database, or you can use a generator function (in case you are unfamiliar, look up the yield keyword).
Citing a Django's manual:
Finally, note that choices can be any iterable object -- not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you're probably better off using a proper database table with a ForeignKey. choices is meant for static data that doesn't change much, if ever.
Why dont you want just export data from the legacy database and to import it into the new one? This could be done periodically, if the legacy database still in use.