0

i have two drop downs from the mysql database in which both are interdependent. what i need is when i select an area from the first drop down, the second drop down as to filter accordingly matching the area from the first drop down. can anyone help me out. [The first drop down is area and the second drop down is zone ].kindly help me out with the changes in this two files. thanks in advance.

models.py

from django.db import models

Create your models here.

views.py

def dashboard_view(request): import mysql.connector

connection = mysql.connector.connect(host='localhost',
                    database='lab_view',
                    user='root',
                    password='inbabtslabuser'
                    )
sql_select_Query ="""select DISTINCT Area from main_labview;"""
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()

sql_select_Query ="""select DISTINCT Zone from main_labview;"""
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records1 = cursor.fetchall()

sql_select_Query ="""select DISTINCT Rack from main_labview;"""
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records2 = cursor.fetchall()
a_list=[]
z_list=[]
r_list=[]
for row in records2:
    r_list.append(row[0])
for row in records:
    a_list.append(row[0])

for row in records1:
    z_list.append(row[0])    
print("\n\n###########################################\nFrom Database:\nAreas: ", a_list)
print("Zones: ",z_list)
print("###########################################\n\n")

context={'Area':a_list, 'Zones':z_list, 'Racks':r_list}

return render(request, 'app/dashboard.html', context)

.html

<form action="/db2/" method="POST"> {% csrf_token %}
<strong><font color="red">Area:</font></strong>
<select id="Area_ID" name="Area" >
<option value="None" selected>Select Area</option>
{% for a in Area %}
    <option value="{{ a }}">{{ a }}</option>
{% endfor %}
</select>


      <br><br>
<strong><font color="red" >Zone:</font></strong>
<select id="Zone_ID" name="Zone" >
<option value="None" selected>Select Zone</option>
{% for z in Zones %}
    <option value="{{ z }}">{{ z }}</option>
{% endfor %}
</select>





        <br> </br>
        <button type="submit" name="try" onClick="location.href='{% url 'db2' %}';" value='submits'>Find Racks</button><br>

        </form>
5
  • Can you share your model.py code snippet Commented Mar 17, 2020 at 10:45
  • hello i have shared my models.py which i havent done anything in that snippet.kindly look into it thank u. @ShahidTariq Commented Mar 17, 2020 at 12:10
  • @andarsh You have nothing in your models.py file. How do you create the models for your app Commented Mar 18, 2020 at 5:49
  • i'm just fetching the data from the query from mysql database , and creating a list and pushing that list of data into the html page. do i really have to create models.py for that. if yes can u please guide me,i'm very new to django ! thank you . @ShahidTariq Commented Mar 18, 2020 at 6:11
  • @andarsh Django provides the best support to deal with backend databases by ORM. You just need to create tables with Django models and than do ORM queries to create update and delete operations. check it out here docs.djangoproject.com/en/3.0/topics/db/models/… Commented Mar 18, 2020 at 6:40

1 Answer 1

1

There is nothing in your models.py file. I believe you are doing it totally the wrong way. You should create tables by Django models not directly by running create table commands in MYSQL shell, like:

from django.db import models

class Area(models.Model):
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name

class Zone(models.Model):
    Area = models.ForeignKey(Area, on_delete=models.CASCADE)
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name

class Rack(models.Model):
    Zone = models.ForeignKey(Zone, on_delete=models.CASCADE)
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name

Moreover, doing a direct MySQL connection is not a good practice in Django as Django automatically handles database connections see here: https://docs.djangoproject.com/en/3.0/topics/db/models/#module-django.db.models

On the top of everything that you are trying to do is best exampled in here: https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html you can follow this blog and try to interdependent dropdowns in Django

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.