Monday, November 24, 2014

Little baffled: object has no attribute 'save' only when running tests.

When writing a test to just try and create an object from my model I get the following error:

line 10, in test_saving_and_retrieving_fishtypes

    test2.save()

AttributeError: 'FishType' object has no attribute 'save'


tests.py

from django.test import TestCase


from trip.models import FishType


class FishType(TestCase):

    

    def test_saving_and_retrieving_fishtypes(self):

        test2 = FishType()

        test2.name = "Testing"

        test2.save()

        

        saved_fish_type = FishType.objects.all()

        self.assertEqual(saved_fish_type.count(), 1)


models.py

from django.db import models


class FishType(models.Model):

    name = models.CharField(max_length=50)

    

    def __str__(self):

        return self.name


Now when using django shell everything works as expected, but for some reason the tests won't work.

>>> from trip.models import *

>>> test_ = FishType()

>>> test_.save()

>>> test_

<FishType: >

>>> test2 = FishType()

>>> test2.name = "Testing"

>>> test2.save()

>>> FishType.objects.all()

[<FishType: >, <FishType: Testing>]


Am I missing something? 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/340f84c4-47b8-4804-8daf-3e65b6f3ed3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment