in the shell, "module object has no attribute Model"

44 views
Skip to first unread message

webcomm

unread,
Aug 2, 2011, 2:34:21 PM8/2/11
to Django users
I am trying to get more comfortable with interactive programming in
the shell... which I have always found a bit awkward and have mostly
avoided in favor of working in files (even for small experiments.)

I'm trying to do something pretty simple:

from django.db import models
class News(models.Model):
title=models.CharField(max_length=200)

It's when I hit return a second time, to get out of the indent, that I
get this error: "module object has no attribute Model". I'm working
in iPython.

Is there an obvious noob mistake I'm making here? I don't run into
this error when I work in a file.

Thanks for your help,
Ryan


webcomm

unread,
Aug 2, 2011, 3:14:09 PM8/2/11
to Django users
I'm also getting "list index out of range" quite often. Here is a
shell session showing the error in context: http://pastebin.com/FBE30SDK

Daniel Roseman

unread,
Aug 2, 2011, 3:38:27 PM8/2/11
to django...@googlegroups.com
On Tuesday, 2 August 2011 20:14:09 UTC+1, webcomm wrote:
I'm also getting "list index out of range" quite often.  Here is a
shell session showing the error in context:  http://pastebin.com/FBE30SDK

I don't know about the 'has no attribute Model', since you don't show the traceback for that. I suspect you just haven't imported what you think you have.
 

But the 'out of range' error is because Django's expecting model class declarations to be inside an app's models.py. It gets certain metadata from there automatically. You can fix this by declaring that metadata explicitly:

  1. In [2]: class MyTest(models.Model):
  2.    ...:     title=models.CharField(max_length=200)
  3.    ...:     class Meta:
       ...:         app_label = 'my_test'

    --
    DR.

webcomm

unread,
Aug 2, 2011, 4:19:14 PM8/2/11
to Django users
On Aug 2, 3:38 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> by declaring that metadata explicitly:
>
>    1. In [2]: class MyTest(models.Model):
>    2.    ...:     title=models.CharField(max_length=200)
>    3.    ...:     class Meta:
>       ...:         app_label = 'my_test'
>

Actually, that seems to have fixed both errors : )

Thx,
Ryan
Reply all
Reply to author
Forward
0 new messages