1

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.

1
  • too many things in one question. First things first, can you create a form, and add that form to your existing view ? Commented Jun 7, 2013 at 16:16

1 Answer 1

1
  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

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

1 Comment

wouldn't it be better to use a ModelChoiceField so that the population is done automatically? (honest question - trying to understand all this stuff at the moment).

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.