Save django CreateView in Python Shell

65 Views Asked by At

I'm trying to write a test for my CreateView view in django. As part fo the test, I want to create a new object through a CreateView class, though I'm unsure how to save the object through tests.py.

models.py

class MyModel(models.Model):
    name = models.CharField(
        max_length = 50,
    )

views.py

class MyCreateView(CreateView):
    model = MyModel

tests.py

from myapp.views import MyCreateView

m = MyCreateView()
m.name = 'John Doe'
# save object here

Neither m.save() nor m.submit() will work. Any suggestions?

1

There are 1 best solutions below

0
Smile23 On

Please refer to Django docs: https://docs.djangoproject.com/en/4.0/topics/testing/

It is well documented and shows how to test views and models.