avaholic
avaholic

Reputation: 120

Creating Dropdown Menu from Model using Django

I tried searching for suggestions on how to solve my problem, but wasn't finding exactly what I was looking for. So, sorry if this has been covered before.

I need to create a new class within existing models.py file to add a new table to an existing database, add code to the existing views.py file to render to a form, and use the existing view.html file to be able to display a dropdown menu that shows all the values in the model and allows the user to select one of those values.

So, I created the table within models.py and need to figure out where to go from here. It looks similar to this:

models.py

class PhoneTable(models.Model):

    phone_number = PhoneNumberField(primary_key=True, max_length=20)
    call_type = models.CharField(choices=CALL_TYPES, max_length=10)
    profile = models.CharField(null=True, blank=True, max_length=4)
    extension = models.ForeignKey(Profile, related_name='extension', max_length=4)

Any advice or direction would be greatly appreciated. If I missed information that would be helpful that I should provide please let me know.

Thank you.

Upvotes: 1

Views: 3359

Answers (1)

Sid
Sid

Reputation: 7631

  1. Write a new view in views.py
  2. Create a new form class with a ChoiceField
  3. Populate this ChoiceField with the data from your database via PhoneTable.objects.all()
  4. Create a template, say phoneview.html
  5. Render this template from your view and send the form as a template parameter

If you don't know how to do these things read Django app tutorial

Upvotes: 1

Related Questions