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

1,087 views
Skip to first unread message

Detectedstealth

unread,
Nov 24, 2014, 5:31:33 PM11/24/14
to django...@googlegroups.com
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? 


Detectedstealth

unread,
Nov 24, 2014, 5:43:45 PM11/24/14
to django...@googlegroups.com
Never mind I need some sleep I named my TestCase with the same name as my model... I can't believe I didn't see that.

class FishType(TestCase):
Reply all
Reply to author
Forward
0 new messages