9

fragment of models.py

class Hardware_type(models.Model):
    type = models.CharField(blank = False, max_length = 50, verbose_name="Type")
    description = models.TextField(blank = True, verbose_name="Description")
    slug = models.SlugField(unique = True, max_length = 255, verbose_name = "Slug")

class Software_type(models.Model):
    type = models.CharField(blank = False, max_length = 50, verbose_name="Type")
    description = models.TextField(blank = True, verbose_name="Description")
    slug = models.SlugField(unique = True, max_length = 255, verbose_name = "Slug")

and now

>>> sw = Software_type.objects.get(slug='unix')
>>> sw
<Software_type: Unix>
>>> hw = Hardware_type.objects.get(slug='printer')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: type object 'Hardware_type' has no attribute 'objects'

I can't see why this happens. Anyone can help me?

Edit:

sorry that did not sent all the code - problem solved. in another class I had

hardware_type = models.ManyToManyField(Hardware_type, verbose_name="Hardware Type")

after change from hardware_type to hw_type - works fine I did not know that can cause this problem.

5
  • 2
    You probably need to reload/refresh something. Commented Oct 16, 2011 at 17:13
  • What's the output of type(Hardware_type)? Commented Oct 16, 2011 at 17:59
  • >>> type(Hardware_type) <class 'django.db.models.base.ModelBase'> Commented Oct 17, 2011 at 6:06
  • 2
    As a side note, you really shouldn't be naming classes with underscores in Python. HardwareType and SoftwareType follow the common convention. Commented Apr 3, 2013 at 10:21
  • I had this problem too. What I did wrong was the give my serializer the same name as the model. So I changed the name of my serializer class to class fooSerializer ... Commented Sep 20, 2019 at 20:11

4 Answers 4

13

If you add a custom manager to a model then the default manager at objects will not be created. Either add it yourself in the class definition, or stick with using the custom manager.

Sign up to request clarification or add additional context in comments.

1 Comment

As this answer suggests, one can add "objects = models.Manager()" to the class definition.
1

Your code works for me:

>>> hw = Hardware_type.objects.get(slug='111')
>>> hw
<Hardware_type: Hardware_type object>

However, using the keyword type might be a little dangerous, and probably you would like to avoid using it.

2 Comments

type isn't a keyword, and using it as an attribute is safe.
Although, it is a built in function. Best to avoid it.
1

it turned out that only began to work in django console,

Later I noticed that I have some old code in forms.py

class Hardware_type(forms.ModelForm):
    class Meta:
        model = Hardware_type

and thus it did not work, it was a bad day for naming classes, etc.

Comments

0

In my case, my model's meta class had abstract=True and i was using model.objects.all(). Since abstract cannot be instantiated, we cannot use this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.