Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a different model form to modelformset_factory

With respect to these models:

class Projects(models.Model):
    projectDescription = models.CharField(max_length=50,blank=True,null = True,)
    status = models.IntegerField(choices = Status_CHOICES, default = 4)
    projectOwner = models.ForeignKey(staff, on_delete=models.CASCADE, blank=True,null = True,)

class Updates(models.Model):
    project = models.ForeignKey(Projects, on_delete=models.CASCADE)
    update = models.CharField(max_length=50,blank=True,null = True,)
    updateDate = models.DateTimeField(default = timezone.now, editable = False)
    addedBy = models.CharField(max_length=35,blank=True,)

I want to create a view that displays forms for all of the current projects. This is easy using a modelformset_factory. But how can I also add an additional form to each of these project form instances so that an Update to the project (foreign key) can be made? Ideally the user makes changes to various projects, adds an update to one or several projects, then submits the form to save all the changes. What I have below seems to be very close, but it is saving an update to each project with the value of whatever I typed into the last form. Which seems to be because the form is not unique. I went down the road of using prefix's which didn't seem to get me anywhere either. help!

update form

class updateForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(updateForm, self).__init__(*args, **kwargs)
    class Meta:
        model = Updates
        fields = ('update',)

view:

def MDSprojects(request):
    projects = Projects.objects.filter(dept = 'Assistive Technology')
    projectFormset = modelformset_factory(Projects, form=EditProjectForm, extra = 0)

    if request.method == 'POST':
        formsetA = projectFormset(request.POST,request.FILES)
        if formsetA.is_valid():
            for f in formsetA:
                formA = f.save(commit = False)
                id = formA.id
                formA.save()
                formsetB = updateForm(request.POST,request.FILES)
                if formsetB.is_valid():
                    formB = formsetB.save(commit = False)
                    project = Projects.objects.get(id = id)
                    formB.project = project
                    formB.save()
                else:
                    print(formsetB.errors)
            return redirect('MDS_Projects')
        else:
            print(formsetA.errors)
    else:
        formsetA = projectFormset(queryset = projects)
        formsetB = updateForm()
        return render(request,'MDSprojectsB.html',{'formset':formsetA,'formsetB':formsetB,})

Template:

<form method="POST" enctype= multipart/form-data>
  {{ formset.management_form }}
  {% csrf_token %}
  {%for form in formset%}
    {{ form.management_form }}
    {% for hidden in form.hidden_fields %}
      {{ hidden }}
    {% endfor %}
  <div class="card border-primary mb-3" style="max-width: 85rem;">
    <div class="card-header text-primary">
       <strong>{{form.instance.projectDescription}}</strong>
    </div>
    <div class="card-body text-primary"> Project Name: {{form.projectDescription}}</div>
    <div class="card-body text-primary"> Status: {{form.status}}</div>
    <div class="card-body text-primary"> Status: {{form.projectOwner}}</div>
    <div class="card-body text-primary"> Status: {{form.priority}}</div>
    <div>
        {{ formsetB.management_form }}
        {% for hidden in formsetB.hidden_fields %}
          {{ hidden }}
        {% endfor %}
        {{formsetB}}
    </div>
  </div>
  {% endfor %}
  <button class="btn btn-success btn" type="submit">Save Changes</button>
 </form>

EDIT:

Perhaps another way of explaining my question is:

If I had the same models as above:

class Projects(models.Model):
    projectDescription = models.CharField(max_length=50,blank=True,null = True,)
    status = models.IntegerField(choices = Status_CHOICES, default = 4)
    projectOwner = models.ForeignKey(staff, on_delete=models.CASCADE, blank=True,null = True,)

class Updates(models.Model):
    project = models.ForeignKey(Projects, on_delete=models.CASCADE)
    update = models.CharField(max_length=50,blank=True,null = True,)
    updateDate = models.DateTimeField(default = timezone.now, editable = False)
    addedBy = models.CharField(max_length=35,blank=True,)

I want to have a view that renders all of the current projects as individual forms so that the user can make changes to the project model in one view. Additionally I want the user to be able to add an update to each project as well.

So for example, if I had 3 projects, I would expect to see 3 forms on the page. Each form would contain the fields to modify this project (so these form fields would be pre-populated with the current values). Then lastly (the part I'm stuck on) would be an additional empty field to add an update to the project.

like image 868
MattG Avatar asked Jun 07 '20 14:06

MattG


1 Answers

Solution I

What you have done is almost correct. You have initiated an UpdateForm but you treated it as if it was a formset. However it's a Form instance. If you alter your code as below you may achieve your goal.

models.py

class Project(models.Model):
    description = models.CharField(max_length=50, blank=True, null=True)
    status = models.IntegerField(choices=STATUS_CHOICES, default=4)
    owner = models.ForeignKey(staff, on_delete=models.CASCADE, blank=True, null=True)
    ...

class Update(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    notes = models.CharField(max_length=50, blank=True, null=True)
    update_date = models.DateTimeField(default=timezone.now, editable=False)
    added_by = models.CharField(max_length=35, blank=True)

forms.py

class CreateUpdateForm(ModelForm):
    class Meta:
        model = Update
        fields = ('notes')


class EditProjectForm(ModelForm)
    class Meta:
        model = Project
        fields = ('__all__')

views.py

def mds_projects(request):
    project_formset = modelformset_factory(Project, form=EditProjectForm, extra=0)

    if request.method == 'POST':
        formset = project_formset(request.POST,request.FILES)
        if formset.is_valid():
            for f in formset:
                project = f.save(commit = False)

                update_form = CreateUpdateForm(request.POST,request.FILES)
                if update_form.is_valid():
                    update = update_form.save(commit = False)
                    update.project = project
                    project.save()
                    update.save()
                else:
                    print(update_form.errors)
            return redirect('MDS_Projects')
        else:
            print(formset.errors)
    else:
        projects = Project.objects.filter(dept='Assistive Technology')
        formset = project_formset(queryset=projects)
        update_form = updateForm()
        return render(request,'MDSprojectsB.html',{'formset':formset, 'update_form':update_form})

MDSprojectsB.html

<form method="POST" enctype= multipart/form-data>
  {{ formset.management_form }}
  {% csrf_token %}
  {%for form in formset%}
    {{ form.management_form }}
    {% for hidden in form.hidden_fields %}
      {{ hidden }}
    {% endfor %}
  <div class="card border-primary mb-3" style="max-width: 85rem;">
    <div class="card-header text-primary">
       <strong>{{form.instance.description}}</strong>
    </div>
    <div class="card-body text-primary"> Project Name: {{form.description}}</div>
    <div class="card-body text-primary"> Status: {{form.status}}</div>
    <div class="card-body text-primary"> Owner: {{form.owner}}</div>
    <div class="card-body text-primary"> Priority: {{form.priority}}</div>
    <div>
        {{ update_form }}
    </div>
  </div>
  {% endfor %}
  <button class="btn btn-success btn" type="submit">Save Changes</button>
 </form>

Solution II

You can also use one form for both models. Since you only want update notes for the Update model you can add additional form field to update project form thus you would not need an additional update form.

forms.py

class EditProjectForm(ModelForm)
    update_notes = forms.CharField(max_length=50, help_text='50 characters max.')
    class Meta:
        model = Project
        fields = ('__all__')

views.py

def mds_projects(request):
    project_formset = modelformset_factory(Project, form=EditProjectForm, extra=0)

    if request.method == 'POST':
        formset = project_formset(request.POST,request.FILES)
        if formset.is_valid():
            for f in formset:
                project = f.save(commit = False)
                update = Update.objects.create(notes=f.cleaned_data['update_notes'], project=project)
                project.save()

            return redirect('MDS_Projects')
        else:
            print(formset.errors)
    else:
        projects = Project.objects.filter(dept='Assistive Technology')
        formset = project_formset(queryset=projects)
        return render(request,'MDSprojectsB.html',{'formset':formset})

MDSprojectsB.html

<form method="POST" enctype= multipart/form-data>
  {{ formset.management_form }}
  {% csrf_token %}
  {%for form in formset%}
    {{ form.management_form }}
    {% for hidden in form.hidden_fields %}
      {{ hidden }}
    {% endfor %}
  <div class="card border-primary mb-3" style="max-width: 85rem;">
    <div class="card-header text-primary">
       <strong>{{form.instance.description}}</strong>
    </div>
    <div class="card-body text-primary"> Project Name: {{form.description}}</div>
    <div class="card-body text-primary"> Status: {{form.status}}</div>
    <div class="card-body text-primary"> Owner: {{form.owner}}</div>
    <div class="card-body text-primary"> Priority: {{form.priority}}</div>
    <div class="card-body text-primary"> Update: {{form.update_notes}}</div>
  </div>
  {% endfor %}
  <button class="btn btn-success btn" type="submit">Save Changes</button>
 </form>

P.S. The code you provided has some violation of both python and django naming conventions. Please try to follow these conventions. I have fixed some of those in my code. For example, you should not name your models plural, you should use camelCase naming for functions and variables.

like image 50
scriptmonster Avatar answered Oct 10 '22 03:10

scriptmonster