I want to build forms and models with dynamic fields like Google Form. But I don't know what type of model.Fields use for this task.
I using Postgres and I thinking about something like Hstore or ArrayField, but I'm not sure.
What is the best solution for this?
There are few solutions available for your task:
EAV (Entity-Attribute-Value) model with django-eav package.
Postgresql HSTORE field but it supports only string type of values.
Postgresql JSON field.
If you use modern PostgreSQL version (9.3+) you should use PostgreSQL JSON fields because JSON supports different data types.
Related
I am new to Django Json Field. I have been creating models and migrating them for now. Now I am introduced to Jsonfield. What I have heard is, it is the best way to mitigate the migrations issue because of jsonfield. It is because, if we had to add fields or remove fields from the model after populating the fields (if we have used other regular fields like chafield and emailfield) in production, there may arrise migration issue which we can avoid if we use jsonfield becaue we can just pass any json data with any number of fields in the jsonfield. So, is this the best way to avoid migration issue?? I am seeking expert advice here, because there is no one I can ask and that is what I have heard.
It seems like this.
class Example(models.Model):
data = models.JSONField(null=False, default=dict)
So, instead of creating two models named Contacts and Feedback for saving the data of contact form and feedback form, I can simply use this same example model and validate to accept any data of many such forms existing in the frontend.
If you want to use JSON just to avoid migrations, then that's not a good idea.
Basically, there are these two rules for using JSON:
If the data doesn't have a strict structure.
If you don't need to query (filter, search, order, etc.) the database using the given data.
Consider this example:
class User:
email = EmailField()
address = JSONField()
The email is in a separate field because we want to easily query the database to check for duplicate sign-ups.
The address is in a JSONField because we won't need to query the database using address data.
However, some applications may require to query using address, for example, to list all users from a particular city. In that case, using JSON will be a bad choice.
I currently use django 3.2 and MySQL as database, I want use MySql Json Field. For this reason use django-mysql third party package in my project.
After create model got this Warning: (django_mysql.W004) django_mysql.models.JSONField is deprecated. HINT: Use django.db.models.JSONField or django-jsonfield-backport instead.
If i use django.db.models.JSONField as json field, django use specific MySQL's JsonField ? Dose any effect on preformance? Which one has best performance on database?
The documentation here, does not mention a difference in performance. So, probably there is none. It looks like the change is only for supporting all databases with the same unique model field. There is a similar issue in this link. So, you can check that, too. As I read, django-jsonfield-backport is for early versions of Django. So, choose according to your version.
I am working on already existing data on relational database. Now question is that how to build models and how to update the tables with new data coming from user (technically django forms)?
Django natively supports creating models for and working with existing data. From the documentation:
Integrating Django with a legacy database
Django will still need to create several of its own tables, but will adapt to use your existing tables. From the doc, you can auto-create models like this:
python manage.py inspectdb > models.py
You'll need to determine whether you want to manage updates to the table structure, but that's getting into details that will be specific to your project.
I need to bulk add m2m fields and looks like django dev version supports bulk.
But they only give example of how to add a single object? I cant find any examples of it being done in bulk.
Also is this method more effective then solution proposed here?
http://grokbase.com/t/gg/django-users/158c8q3ty3/bulk-add-m2m-relationship-for-multiple-instances (
I'm in the process of choosing a Framework for a new project. I have basically the database schema developed(I have this schema running in other PHP webapps already).
In this project I will need to basically search the database schema with Solr. The database schema is a little bit complex to define models in Django, so I think the only option I have is to execute SQL directly... my doubt is about Haystack/Solr... It is possible to query Haystack/Solr when I have no Django Models defined?
PS: I'm new to Django, I have never userd Haystack.
Haystack is pretty tightly coupled to the Django ORM. If you're not using Django models, I don't think Haystack is suitable. I've only used Haystack briefly, so I might wrong.
From the Haystack docs:
When should I not be using Haystack?
Non-Model-based data. If you just want to index random data (flat files, alternate sources, etc.), Haystack isn’t a good solution. Haystack is very Model-based and doesn’t work well outside of that use case.
I never used haystack, but you can always perform raw sql queries.
Have a look on documentation:
https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly