# two models into one form into view

**URL:** <https://forum.djangoproject.com/t/two-models-into-one-form-into-view/15731>\
**Category:** Forms & APIs\
**Created:** [August 30, 2022, 11:48pm UTC](https://forum.djangoproject.com/t/two-models-into-one-form-into-view/15731 "2022-08-30T23:48:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DoriDoro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doridoro/32/5014_2.png) [@DoriDoro](https://forum.djangoproject.com/u/DoriDoro)\
**Post date:** [August 30, 2022, 11:48pm UTC](https://forum.djangoproject.com/t/two-models-into-one-form-into-view/15731/1 "2022-08-30T23:48:06Z")

</div>

Hey everyone,

The idea is to have this:

 ![Screen Shot 2022-08-31 at 01.26.46](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/9/95673b3ed4ef796a681b97c88f6d997c77ad9669.png)  
This will be kind of the demo for the Todo list. To create a list you have to click on the Create List button. That is now the idea.

I created the models.py:

```auto
from django.db import models

class TodoItem(models.Model):
	item = models.CharField(max_length=200)

	def __str__ (self):
		return self.item
		

class TodoList(models.Model):
	title = models.CharField(max_length=200)
	items = models.ManyToManyField(TodoItem)
	completed = models.BooleanField(default=False)

	def __str__ (self):
		return self.title

```

forms.py:

```auto
from django import forms
from .models import TodoList, TodoItem

class TodoListForm(forms.ModelForm):
	class Meta:
		model = TodoList
		fields = ['title', 'items']

class TodoItemForm(forms.ModelForm):
	class Meta:
		model = TodoItem
		fields = ['item']

```

views.py:

```auto
from django.shortcuts import render, redirect
from django.contrib import messages
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.base import TemplateView

from .models import TodoList, TodoItem
from .forms import TodoListForm, TodoItemForm

class TodoListUpdate(UpdateView):
	model = TodoList
	fields = ' __all__'
	tempate_name_suffix = '_update_form'

class TodoListUpdateView(TemplateView):
	template_name = '_update_form.html'

	def get_context_data(self, **kwargs):
		context = super().get_context_data(**kwargs)
		todo_list = get_object_or_404(TodoList, pk=self.kwargs['pk'])
		context['todo_list'] = todo_list
		context['todo_list_form'] = TodoListForm(instance=todo_list)
		context['todo_item_form'] = TodoItemForm(instance=todos.todoItem)

		return context

```

project/urls.py:

```auto
from django.contrib import admin
from django.urls import path

from ToDoro_app import views

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", views.home, name='home'),
    path("create/<int:pk>", TodoListUpdateView.as_view(), name='create_update_form'),
    path("edit/", views.edit, name='edit'),
    path("complete/<int:todo_id>", views.complete, name='complete'),
    path("incomplete/<int:todo_id>", views.incomplete, name='incomplete'),
]

```

But I get the error in terminal:

```auto
path("create/<int:pk>", TodoListUpdateView.as_view(), name='create_update_form'),
NameError: name 'TodoListUpdateView' is not defined

```

Ok, I have not defined a function in views.py for TodoListUpdateView but I thought that the TemplateView will do the trick. What do I missing?

Thanks  
Doro

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 31, 2022, 12:05am UTC](https://forum.djangoproject.com/t/two-models-into-one-form-into-view/15731/2 "2022-08-31T00:05:14Z")

</div>

You need to import the view in the urls.py file.

---

<div class="post-metadata">

**Author:** ![DoriDoro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doridoro/32/5014_2.png) [@DoriDoro](https://forum.djangoproject.com/u/DoriDoro)\
**Post date:** [August 31, 2022, 12:21am UTC](https://forum.djangoproject.com/t/two-models-into-one-form-into-view/15731/3 "2022-08-31T00:21:03Z")

</div>

LOL that seems easy 🤣

Thank you
