django - Testing model - how to create records -
according https://docs.djangoproject.com/en/1.9/topics/testing/overview/ :
from django.test import testcase myapp.models import animal class animaltestcase(testcase): def setup(self): animal.objects.create(name="lion", sound="roar") animal.objects.create(name="cat", sound="meow") def test_animals_can_speak(self): """animals can speak correctly identified""" lion = animal.objects.get(name="lion") cat = animal.objects.get(name="cat") self.assertequal(lion.speak(), 'the lion says "roar"') self.assertequal(cat.speak(), 'the cat says "meow"')
i want create setup model class:
class post(models.model): author = models.foreignkey('auth.user') tags = models.manytomanyfield('bloguserplane.tag') title = models.charfield(max_length=200) text = models.textfield() created_date = models.datetimefield( default=timezone.now) published_date = models.datetimefield( blank=true, null=true)
this not working setup:
def setup(self): post.objects.all().create(author=user(), tag=models.manytomanyfield('bloguserplane.tag'), title="title1", text="text1", created_date=none, published_date=none)
what correct way create records of model manytomanyfield , foreginkey?
if want enter value in foreignkey or manytomany field ,you first need import value . example if want enter value in author field ,
from django.contrib.auth.models import user user = user.objects.get(username = 'your_username') post.objects.create(author = user)
Comments
Post a Comment