D
D
Dmitry S2016-09-17 20:26:14
MySQL
Dmitry S, 2016-09-17 20:26:14

Is it possible to create dependent selects in django without reloading the page?

Good day!
The point is the following.
There are several MySQL tables linked via FK: Countries, Federal Districts, Regions, Cities.

#models.py
class Country(models.Model):
  country_name = models.CharField(max_length=200, verbose_name='Страна')

  def __unicode__(self):
    return self.country_name

  class Meta:
    verbose_name = 'страна'
    verbose_name_plural = 'Страны'

class District(models.Model):
  district_name = models.CharField(max_length=100, verbose_name='Федеральный округ')
  district_country = models.ForeignKey('Country', verbose_name='Подчинение страны')

  def __unicode__(self):
    return self.district_name

  class Meta:
    verbose_name = 'федеральный округ'
    verbose_name_plural = 'Федеральные округа'

I display data from this table and some others through views.py:
#views.py
def CabinetView(request, id):
  cab = Cabinet.objects.all().filter(id=id) #Выводится список нужных кабинетов
  cab_type = CabinetTypes.objects.all() #... их типов
  country = Country.objects.all() #... список стран
  district = District.objects.all() #... список фед. округов
  region = Region.objects.all() #... список районов
  city = City.objects.all() #...список городов
  rendering = {
  'cab': cab,
  'cab_type': cab_type,
  'country': country,
  'district': district,
  'region': region,
  'city': city,
  }
  return render_to_response('cabinet.html', rendering)

into the template into the form where the dependent selects should be:
<!-- cabinet.html -->
      <tr>
        <th>Страна</th>
        <td>
          <select name='cab_coutry' class='forinput_edit' id='select-1'>
            {% for i in country %}
              <option value='{{ i.id }}' required>{{ i.country_name }}</option>
            {% endfor %}
          </select>
        </td>
      </tr>
      <tr>
        <th>Федеральный округ</th>
        <td>
          <select name='cab_district' class='forinput_edit' id='select-2'>
            {% for i in district %}
            <option value='{{ i.id }}' required>{{ i.district_name }} ({{ i.district_sname }})</option>
            {% endfor %}
          </select>
        </td>
      </tr>

The form should work in theory like this: when choosing a country without reloading the page, the list of feds should be loaded in the next select. counties associated with the selected country. Etc. The list of districts has been loaded - the next select will load the list of regions, and then the cities in the selected region. That is 4 dependent select'a.
Is it possible to implement this with django? Despite the fact that there are many sites on the Internet where such a "system" is implemented, information about the implementation itself is rather scarce.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tema_sun, 2016-09-17
@tema_sun

Django alone is indispensable here, ajax is needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question