How can i make people selected 2 choices between 3 options? - django

I want users to choice 2 selection of my checkbox how can i do that by using Django?
I ve tried that
forms.py
class KaydolForm(forms.ModelForm):
secim = (('Bk','banka kartı'),('kk','Kredi Kartı'),('cek', 'çek'))
secims=forms.ChoiceField(choices=secim, widget=forms.RadioSelect)
but i can choose only one of them

https://docs.djangoproject.com/en/3.1/ref/forms/fields/#multiplechoicefield
Read this, in your form it would be like this:
class KaydolForm(forms.ModelForm):
choices = (
('Bk', 'banka kart1),
('kk', 'Kredi'),
('cek', 'cek')
)
secims = forms.MultipleChoiceField(choices=choices)

You should use a MultipleChoiceField. On this field you can select multiple options.
Then you should also use a widget (representation on webpage) that accepts multiple choices. You could use CheckboxSelectMultiple or SelectMultiple
So change your code to:
class KaydolForm(forms.ModelForm):
choices_list = (('Bk','banka kartı'),('kk','Kredi Kartı'),('cek', 'çek'))
choice_field = forms.MultipleChoiceField(choices=choices_list, widget=forms.SelectMultiple)
The user could now however check one, two or three options. You must write a own validator to check, if exactly two are checked. For more detail see here

Related

How can I make a choice field with steps in Django admin

I'm trying to make something like the image below. Let's say that I have 3 multiple choice fields like the following. If I choose bags from choices_first, I want choices_for_bags to show up as the next choice step.
The below image is one of the fields for Django default user model. It's for another purpose. I'm looking for something like that. As I choose something on the left, I want something to show up on the right based on the choice on the left. Does Django admin provide anything like that as default or is there any library for that?
choices_first = (
('clothing', 'Clothing'),
('bags', 'Bags'),
)
choices_for_clothing = (
('tops', 'Tops'),
('bottoms', 'Bottoms'),
)
choices_for_bags = (
('handbags', 'Handbags'),
('backpacks', 'Backpacks'),
)
As far as I know django-smart-selects provide ChainedManyToManyField option which should be similar to User Permissions scenario since its ManyToMany field, but I'm not sure if it will work as you need.

Django MultiSelectField with multiple fields per choice

I'm using MultiSelectField in my form, which allows the user to select multiple choices:
models.py
physical_limits = MultiSelectField('Physical Limitations', choices=PHYSICAL_LIMIT, max_length=255)
screenshot link
However, I would like to add few additional fields per choice, e.g. min value, max value, units, etc (for the user to fill in, for each choice he makes).
For example, if the user selects option (2), which is "Solid", he will then be able to fill in additional corresponding "sub-fields" for this selection.
In some way, I feel like I'm looking for a different field type, which acts more like a table or array, for the user to fill. I couldn't find such a thing though..
"Vision"
Can you please guide me how to achieve that goal?
Thanks ahead,
Shahar

How do I add a blank default option to a select widget?

I'm attempting to create a search page for a database that I am maintaining by creating a dynamically maintained select box of all of the unique values of a few of my fields.
After much time and thought, I've decided that the best way to create a search form for my database would be to do so in views, resulting in something equivalent to this:
search_form = modelform_facotry(my_model,
fields=('field_one', 'field_two',),
widgets={'field_one': Select(choices=((datum.field_one, datum.field_one) for datum in my_model.objects.distinct('field_one'))),
'field_two': Select(choices=((datum.field_two, datum.field_two) for datum in my_model.objects.distinct('field_two'))),
}
)
This works great! Except that I can't figure out how to include a blank option... The choices have to be created in a loop like that, so I can't just add a blank choice as many solutions suggest.
I've been pulling out my hair over this for a while now, because it seems like adding a blank option to a select widget would be an easy fix, but evidently not. Any help would be appreciated.
Edit: I should add that my fields are all CharFields
After playing around with it for a while, I discovered what I needed to do:
FIELD_ONE_CHOICES = (('', '-----'),)
for datum in my_model.objects.distinct('field_one'):
FIELD_ONE_CHOICES += ((datum.field_one, datum.field_one),)
FIELD_TWO_CHOICES = (('', '-----'),)
for datum in my_model.objects.distinct('field_two'):
FIELD_TWO_CHOICES += ((datum.field_two, datum.field_two),)
search_form = modelform_factory(my_model,
fields=('field_one', 'field_two'),
widgets={'field_one': Select(choices=FIELD_ONE_CHOICES),
'field_two': Select(choices=FIELD_TWO_CHOICES),
}
)
Tuples can be frustrating...

Limit django queryset by another related table

Lets say I have 2 django models like this:
class Spam(models.Model):
somefield = models.CharField()
class Eggs(models.Model):
parent_spam = models.ForeignKey(Spam)
child_spam = models.ForeignKey(Spam)
Given the input of a "Spam" object, how would the django query looks like that:
Limits this query based on the parent_spam field in the "Eggs" table
Gives me the corresponding child_spam field
And returns a set of "Spam" objects
In SQL:
SELECT * FROM Spam WHERE id IN (SELECT child_spam FROM Eggs WHERE parent_spam = 'input_id')
I know this is only an example, but this model setup doesn't actually validate as it is - you can't have two separate ForeignKeys pointing at the same model without specifying a related_name. So, assuming the related names are egg_parent and egg_child respectively, and your existing Spam object is called my_spam, this would do it:
my_spam.egg_parent.child_spam.all()
or
Spam.objects.filter(egg_child__parent_spam=my_spam)
Even better, define a ManyToManyField('self') on the Spam model, which handles all this for you, then you would do:
my_spam.other_spams.all()
According to your sql code you need something like this
Spam.objects.filter(id__in= \
Eggs.objects.values_list('child_spam').filter(parent_spam='input_id'))

Django: Deleting user selected entries from a database

I have a Django app that displays a list of rows in a table to the user. Each row maps to an entry in a database. I want to let the user select the rows they would like deleting by adding a checkbox to the end of each row and a delete button ( similar to how gmail lets you delete multiple mail messages). I can't quite figure out how to write the view in terms of finding out which rows were selected and how to map these to the IDs of the entries that need deleting from the database. A simple code snippet showing how to do this would be greatly appreciated.
UPDATE:
I've found this code snippet that I think should do the trick
You can use the CheckboxSelectMultiple widget to auto-generate the corresponding HTML code so you don't have to do it manually.
You can define your form like so:
class UsersForm(forms.Form):
users = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=[QuerySetIterator(Users.objects.all(), "", False)], label="")
Another advantage is that you also get validation for free.
Create a formset and pass can_delete = True to the constructor. Then, in the template,
{{formset}}