How can I make multiple value field? - django

How can I make a field that can accept more than one input and connect it to my database?
For example : I want to make a field that is called 'user_skills' and the user can enter more than skil in that field .. How can I do that?

Depends on the Database you are using. If you are using postgresSql you can do this with ArrayField link
Other possible options can be use JsonField or try to use normalize Model where you can store pre-define value's of corresponding field and can access them with ManyToManyField.

Related

Django setup model with multi value integer field

How can we setup a field in a model to accept more than on value?
We simply can make relation fields using foreign key or many to many but in case of simple int or float or any simple variable type , how can we achieve multi value field?
If it is only about the database field, you could simply serialize your values into a string, e.g. values 1, 2, 3 become "1,2,3" in the field. Simply overwrite the getter and setter for that field (using #property), to serialize and unserialize the values each time the field is accessed.
Another approach is to use a JSONField (doc). This has wider support (for example searchability via querysets, at least using PostGreSQL. Also several 3rd party JSON form fields. You'd need to validate that the JSON supplied was a list of integers).

Django Model become huge

In my project few models has many fields like more than 25. Like i have a model name PeriodOfStay. and it fields are like
date_of_entry
i94_number
port_of_entry
city ....etc (please check the image for all field)
also it has many boolean fields . in one form user can multiple options.
most of the fields are optional.
so i am confused should i put all the fields in one model. Is it best practice. I don't want split one model to more and use OneToOne Relation cause in that case i need to break up many models cause most of the models in my project are like this also i need to send all data at once in a single request.
I just need to save data and show data to user. in some case i need to search by some field . Like in this form i need to search by i94_number.
Is using JsonField is ok for this problem cause i need to search & filter in some case.
I appreciate any help. Advance Thanks For Help.
Regarding your question about when to use one-to-one relations:
https://dba.stackexchange.com/a/15405
I think a JsonField is not what you want if you want to search & filter based on some fields. Keeping it in normal fields will make it faster
Some related resource that might be interesting: https://en.wikipedia.org/wiki/Database_normalization

User generated item multiple choice field Django

I am looking for a way to have a multiple choice field populated with choices made by a user. For example, they could have the following 3 entries: Yes, No, Unsure. I want a way to be translate this to a model.
I understand this can be done with pre-defined options using a CharField, or ChoiceField, but I haven't seen anything with "dynamic" data, such as user-generated data.
I think you shouldn't use choice fields in this case, because everytime a user creates a new option, you'll have to run a new migration.
Maybe you can create a UserOption model and create a new obj everytime a user creates a new option. Then fetch all the options the user has created when he needs to choose between them.
You can't apply user choices to a DB table. However, you could store a user's choices somewhere, such as a User's Profile, and use them in a dynamically constructed form with a ChoiceField to constrain what he can put into a particular database column / model field.
Could be a PITA if he decides to delete a choice and then wants to edit the (now invalid) data in one of his records.

A django field with two fields. Need opinion

There is a scenario where I want to store two types of information for a field. For example, consider a field namely "name". This field should two types of the following information:-
Boolean - to store whether the user wants this field to be shown or not.
Label - if the user wants to change the label to show.
I was wondering to solve this by using a model. Any suggestions?
You can define fields on other model and use them with genericForeignKey

Django show options

I have a database filled with values for first_name. I also made a form, which calls for an input of first_name. How can i give the user suggestions as they type of names that are already in the database?
Also, if i have someone entering 2 in one integerfield, and a 3 in another integerfield, how can i autocompute the product and show it in real-time?
The package https://github.com/yourlabs/django-autocomplete-light seems to be what you're looking for.
It allows the end user to type some characters, after which it will be autocompleted/suggested from what's already in the DB.
For autocomputing the two intergerfields, I'd go for client-side (Javascript).