Home > Mobile >  How to import values from database into a OptionMenu Django Python
How to import values from database into a OptionMenu Django Python

Time:01-26

I have a models page here.

 from django.db import models

class Trainee(models.Model):
TraineeID = models.AutoField(primary_key=True)
Name = models.CharField(max_length=50)
Course = models.CharField(max_length=20)
BatchNo = models.CharField(max_length=15)
DateofBirth = models.CharField(max_length=30)
ContactNo = models.CharField(max_length=20)
ContactAddress = models.CharField(max_length=80)
EmailAddress = models.EmailField()
class Meta():
    db_table = "Trainee"

class Course(models.Model):
CourseID = models.AutoField(primary_key=True)
CourseName = models.CharField(max_length=20)
CourseDuration = models.CharField(max_length=30)
CourseCost = models.CharField(max_length=50)
class Meta():
    db_table = "Courses"

I made a html page where I can enter data and save Trainee. I want to make it so that Course will be a OptionMenu not a charfield that will have the options <select> only from the data saved in Courses table. So if there are saved data's like-Java, Python, C etc in Courses table then the option menu Course will only have these on it. And if they are deleted or new ones are added then the optionmenu will change accordingly. This is my html page where I can enter the data and save it:

{% extends "MyTestApp/base.html" %}
{% block body_block %}
{% load static %}                  
<link rel="stylesheet" href="{% static '/css/bootstrap.min.css'%}" />
<form method="post" action="/trainee/">
{%csrf_token%}

    <div >

        <br/>

        <div >
            <label ></label>
            <div >
                <h3> Enter Trainee Information </h3>
            </div>
        </div>

        <div >
            <label > Name: </label>
            <div >
                {{form.Name}}
            </div>
        </div>

        <div >
            <label  for="Course"> Course: </label>
            <div >
                    <select name="Course" id="Course">
                        
                            <option value="{{courses.CourseName}}">{{courses.CourseName}}</option>
                        
                    </select>

            </div>
        </div>

        <div >
            <label > BatchNo: </label>
            <div >
                {{form.BatchNo}}
            </div>
        </div>

        <div >
            <label  for="DateofBirth"> Date Of Birth: </label>
            <div >
                <input type="date" name="DateofBirth" id="DateofBirth">
            </div>
        </div>

        <div >
            <label > ContactNo: </label>
            <div >
                {{form.ContactNo}}
            </div>
        </div>

        <div >
            <label > ContactAddress: </label>
            <div >
                {{form.ContactAddress}}
            </div>
        </div>

        <div >
            <label > EmailAddress: </label>
            <div >
                {{form.EmailAddress}}
            </div>
        </div>

        <div >
            <label ></label>
            <div >
                <button type="submit" > Submit </button>
            </div>

        </div>
    </div>
</form>
{% endblock %}

I Hope I have made myself clear. Any help will be appreciated. I have been stuck at this for the past month.

CodePudding user response:

If I understand the issue here correctly you are looking for:

class Trainee(models.Model):
     ...
     course = models.ForeignKey('Course', on_delete=models.SET_NULL)
     ...

Read more about it here: Django's ForeignKey. Btw. if you want to follow recommended PEP8 rules, and believe me you want to, model fields and variables should be snake_case (lower case).

Your form view in views.py might need to have courses = Course.objects.all() in it's context, depends on what you have in there.

views.py:

SomeView(AnyView):
    ...
    extra_context = {'courses': Course.objects.all()}
    ...

Then html form template:

...
<select name="course" id="id_course">

    {% for course in courses %}               
        <option value="{{ course.id }}">{{ course.course_name }}</option>
    {% endfor %}

</select>
...

It is usually better to pass value of object.id instead of object.name. Names should be for humans, ids for our algorithms.

  •  Tags:  
  • Related