user9221735
user9221735

Reputation:

Django ModelForm not displaying all fields

I'm trying to build forms linked to a PostgreSQL database using Django ModelForms. The template is rendering two of the fields(the ones with ManyToMany relationships), but it only gives me an empty box for "title".

This is my forms.py:

Forms.py:

class ProgramForm(forms.ModelForm):
class Meta:
    model = Program
    fields = ['function','task', 'title']
widgets = {
    'function' : forms.Select,
    'task' : forms.Select,
    'title' : forms.Select,
    }

This is my Models.py:

class Program(models.Model):
    title = models.CharField(max_length=255)
    function = models.ManyToManyField(function, related_name='programs')
    task = models.ManyToManyField(Task, related_name='programs')

    def __unicode__(self):
        return self.title

class Task(models.Model):
    tasknum = models.CharField(max_length=20)
    taskname = models.CharField(max_length=100)
    task_num_name = models.CharField(max_length=100)
    function = models.ForeignKey(Function, related_name="tasks")

    def __unicode__(self):
        return self.task_num_name

class Function(models.Model):
    function = models.CharField(max_length=50)
    function_abrev = models.CharField(max_length = 25)

    def __unicode__(self):
        return self.function

Views.py:

def main(request):
    return render (request, 'assignments/main.html')

def add_program(request):
    form = ProgramForm()
    return render (request, 'assignments/ad_form.html', {"form":form})

def link(request):
    if request.method == 'POST':
        form = ProgramForm(request.POST)
        if form.is_valid():
        return HttpResponse("we maybe getting somewhere") 
    else: 
        return HttpResponse("keep working")

I need a couple of things to happen:

  1. I need for the "title" to render in the html page as a scroll down(the same way "function" and "task" appear.

  2. I need to be able to save the relationships. The models are populated with all the information required with the exception of the relationships. The objective is for a staff member to be able to chose a "function", for that choice to act as a filter for the "task" scroll down(function and task have a OneToMany), and then allow them to choose any programs they want to add to their portfolio.

Any help will be much appreciated.

Upvotes: 0

Views: 2268

Answers (2)

user9221735
user9221735

Reputation:

I was able to do a query from views.py and pass if to the template.

Views.py 

def function_page(request, Function_id):
assignments = Function.objects.get(id=Function_id)
programs = assignments.programs.all()
context = {
   'assignments': assignments,
   'programs' : programs
}
return render (request, 'function.html', context)

HTML

 {% for program in programs %}
      <option value="{{program.title}}">{{program.title}}</option>
      {% endfor %}

Upvotes: 0

Rohan Varma
Rohan Varma

Reputation: 1195

1. Title field in form

For this, I don't quite understand how the title field could be a scroll down the same way function and task are. Function and task are drop downs because they are manytomany fields linked to other models, meaning that the user has to pick which other objects in the Functions model and the Tasks model are to be linked. The title field, on the other hand, is just a CharField and so there is no defined set of things for the user to pick from. To allow the user to enter in the title for the Program, you should change the widget for title to Textarea() as such:

forms.py

from django.forms import ModelForm, Textarea

class ProgramForm(forms.ModelForm):
    class Meta:
        model = Program
        fields = ['function','task', 'title']
    widgets = {
        'function' : forms.Select,
        'task' : forms.Select,
        'title' : Textarea(),
    }

2. Save the Program from the form

To save the Program created by the user on staff member, simply add form.save() to your link(request) function:

views.py

def link(request):
    if request.method == 'POST':
        form = ProgramForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponse("we maybe getting somewhere") 
        else: 
            return HttpResponse("keep working")

Hope this helps!

Upvotes: 1

Related Questions