Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:49:21+00:00 2026-06-18T14:49:21+00:00

Am new to programming in Python and Javascript and I have been learning Python

  • 0

Am new to programming in Python and Javascript and I have been learning Python for few months now and love it. I have been playing with django which is cool I was wondering how I can make this model work with a Iavascript. I’ll like someone to explain as much as the code involved as I just what to have a full understanding of the process from django to Javascript.

I want to dynamically is CarModel.objects.filter(make ='somename') or just ‘somename’.

This is a test model am using since its similar to the Javascript I use for tutorial from online (YouTube) the scripts is below as well:

class Make(models.Model):
    name  = models.CharField(max_length=50,blank=True,null = True)
    #so so so so

class CarModel(models.Model):
    name  = models.CharField(max_length=50,blank=True,null = True)
    make = models.ForeignKey(Make,blank=True,null = True)

Now how will you pass say something like this to your Javascript?

class Model(ModelForm):
    make = forms.ModelChoiceField(queryset= Model.objects.none(), required=True)

    def __init__(self, somemake,*args, **kwargs):
        super(Model, self).__init__(*args, **kwargs)
        self.fields['property'].queryset = Model.objects.filter(make = somemake)

    class Meta:
        model = Model
        exclude= ('make')

<script type="text/javascript">
function populate(s1,s2){
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
s2.innerHTML = "";
if(s1.value == "Chevy"){var optionArray = ["|","camaro|Camaro","corvette|Corvette","impala|Impala"];
    } 
else if(s1.value == "Dodge"){
    var optionArray = ["|","avenger|Avenger","challenger|Challenger","charger|Charger"];
    } else if(s1.value == "Ford"){
    var optionArray = ["|","mustang|Mustang","shelby|Shelby"];
    }
    for(var option in optionArray){
    var pair = optionArray[option].split("|");
    var newOption = document.createElement("option");
    newOption.value = pair[0];
    newOption.innerHTML = pair[1];
    s2.options.add(newOption);
    }
    }
</script>

and html here

Choose Car Make:
    <select id="slct1" name="slct1" onchange="populate(this.id,'slct2')">
          <option value=""></option>
          <option value="Chevy">Chevy</option>
          <option value="Dodge">Dodge</option>
          <option value="Ford">Ford</option>
    </select>

   Choose Car Model:
  <select id="slct2" name="slct2"></select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:49:22+00:00Added an answer on June 18, 2026 at 2:49 pm

    If you want to do in JS, this is how I would solve the problem.

    First create a template that contains the following for the 2 select lists.

    <html>
    <head>
    <script>
    var json = {
        "Chevy": ["chev1", "chev2", "chev3"],
        "Dodge": ["dodge1", "dodge2", "dodge3"],
        "Ford": ["ford1", "ford2", "ford3"]
    };
    
    function carMake () {
        select = document.getElementById('slct1');
        select.options.length = 0;
        for(make in json) {
            select.options[select.options.length] = new Option(make, make);
        }
    }
    
    function carModel(sel) {
        var car_make = sel.options[sel.selectedIndex].value
        select = document.getElementById('slct2');
        select.options.length = 0;
        for(var i=0;i<json[car_make].length;i++) {
            select.options[select.options.length] = new Option(json[car_make][i], i);
        }
    }
    </script>
    </head>
    
    <body>
    Choose Car Make:
    <select id="slct1" onchange="carModel(this)"></select>
    <script> carMake(); </script>
    
    Choose Car Model:
    <select id="slct2" name="slct2"></select>
    </body>
    </html>
    

    The above JS will read in the JSON object and update the Car Make when ever the dynamically populated Car Model select field is changed.

    To generate the JSON object using your supplied Model you need to do the following:

    In the view.py file:

    from <your-app>.models import Make
    from <your-app>.models import Model
    import json
    
    json_dict = {}
    for car_make in Make.objects.all():
        json_dict[car_make] = Model.objects.filter(make=car_make)
    json_data = json.dumps(json_dict)
    

    Then you take json_data and and add that to your response render context.

    Finally alter the above template so that the JS variable json will be rendered to contain the JSON object passed from the view to the template.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to python programming and have yet to buy a textbook
I am fairly new to programming and to python and wxpython. I have looked
I am new to programming and am learning Python as my first language. I
I'm new to programming, I have some basic python programming from college, I am
I am new to programming in python,but have programmed in C. I am working
I am new to programming in python and need help doing this. I have
I've been programming Python a while, but DJango and web programming in general is
I'm been programming with Python in AppEngine for the last months, and I need
I am new to computer programming and have some experience programming with python. I
I am new to programming and Python. I have a very basic python script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.