Django show options - django

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).

Related

Is there a form field in Django that allows you to enter multiple values one at a time?

I could also use a TextField, but that's not ideal from UX point of view. I would like to know if there's a field that allows the user to enter multiple custom strings (not from a pre-defined list of options), one at a time, where, to enter a string, one needs to press enter, these values are then passed as a list of strings (or other types) to the model, which defines an ArrayField.
I've searched for some time, but I couldn't find it. I'm not really an expert in Django, but I suppose such a field must exist.
If not, I suppose I could create a custom form field that does that.
Here's what I mean.
In the example, 1 was written, then the user pressed ENTER, and the option was added; then the same thing happened to add 2; the important thing to note is that 1 and 2 do not come from a predefined set of values, but were custom strings entered by the user, which can also be removed after having been entered.
If I've got your question right, you want a ArrayField for your form. Take a look at SplitArrayField:
from django.contrib.postgres.forms import SplitArrayField
class SthForm(forms.Form):
arr_field = SplitArrayField(forms.CharField(max_length=100))

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.

Database Structure for Preference selection

I have two models, user and course who should be matched. To provide a basis for this matching, both models can rank the other one. I'm trying to figure out a database structure to efficiently store the preferences. The problem is, that the number of ranks is unknown (depending of number of users/courses).
I'm using Django with Postgres, if this helps.
I tried storing the preferences as a field with a pickled dictionary, but this causes problems if a model stored in the dict is updated and takes a long time to load.
I'm thankful for any tips or suggestions for improvement.

Overcoming Exclude List Limit Size

I'm trying to make a query using Django's Exclude() and passing to it a list, as in:
(...).exclude(id__in=list(top_vip_deals_filter))
The problem is that, apparently, there is a Limit -- depending on your database --on the size of the list being passed.
Is this correct?
If so, How to overcome this?
If not, is there some explanation to the fact that queries silently fail when the list size is big?
Thanks
If the top_vip_deals_filter comes from the database, you can set an extra where in the query:
(...).extra(where=['model.id not in select blah blah'])
(put your lowercase model name instead of model.)
You can do better if the data model allows you to. If you can do it in SQL, you probably can do it in django.