Django.Theory: Implement multiple checkboxes?

0 like 0 dislike
Hi all.

I'm writing a service about the sale/rental of real estate.

Interested in the following question:

There is an object let's call it for example "Home",

the house has the following parameters:

1. "Name"

2. "Heating type" with the following choices
  • Gas heating
  • Central heating
  • Oil(diesel) heating
  • Electric heating
  • Underfloor heating
  • Solar panels
  • Wood heating

3. The "kitchen"
  • Built-in kitchen
  • Kitchenette
  • Kitchen-dining room
  • Kitchen island
  • Open kitchen

and so on.

All the parameters are strictly defined in the code, i.e. there is no need to create/edit in the database.

All the options on the form should be implemented as a checkbox with the option

multiple choice.

Actually a question how to implement a model in django:

1. Form persaonal multiple "checkboxes"

2. To search by these parameters, for example find a home with "Gas heating" and "kitchen"

3. How to keep in the database marked checkbox?

Option number one:

Implement all using ForeignKey

Minus: expensive, a lot of queries...etc

Option number two:

Save in database the entry "kitchen: option1, option2, option4"

But with this option going to search, display the form with the note fields.

I would be grateful for advice.
by | 4 views

2 Answers

0 like 0 dislike
>>Implement everything using ForeignKey
for multiple select, you need a ManyToManyField
(1, 'kitchen'),
(2, 'Kitchen'),
(3, 'Kitchen'),
(4, 'Kitchen island'),
(5, 'Outdoor kitchen'),
class KitchenChoices(models.Model):
options = models.CharField(max_length=100, blank = True, choices=KITCHEN_OPTIONS)
class Apartment(models.Model):
kitchen_type = models.ManyToManyField(KitchenChoices)
in template this field is display automatically and to make iteration over options and build checkboxes with appropriate Labelle, and value...
question, do you need to save the form from the frontend, or just do a search for fields... hence the options branch...
if save is not necessary, then the admin will get a multiple select to save the options... if you need something
when you save the form
class Apartment(models.Model):
kitchen = models.CharField(max_length=100, blank = True, choices=KITCHEN_OPTIONS)
in template this field is display automatically and to do iteration
0 like 0 dislike
If the set of checkboxes fixed, need to start in the model "home" field for each of the checkboxes.
In the form of hang on each field checkbox. Only the standard template does not fit, will have to impose their own, so the checkboxes show up multiple lists. But it's not terrible.
The search is trivial — all in one table.
110,608 questions
257,186 answers
32,830 users