Some creating Forms from models snags.

25 views
Skip to first unread message

Don Fox

unread,
Jul 8, 2014, 9:31:48 PM7/8/14
to django...@googlegroups.com
In the Creating forms from Models documentation https://docs.djangoproject.com/en/1.6/topics/forms/modelforms/ I'm having some problems.

First I'm placing the code from the first block (green section), where the class ArticleForms is defined in the shell in a forms.py file in a myapp app of a project.

Second, the models in the second and third green blocks are located in the myapp/models.py with the class AuthorForm and class BookForm classes in the from the first block commented out.  


On arriving at the 4th green block that has
>>> from myapp.models import Article
>>> from myapp.forms import ArticleForm
I get this response from a syncdb:



File "/Users/donfox1/Projects/DjangoExplorations/PycharmProjects/classBased/myapp/forms.py", line 17, in <module>

    from myapp.models import Article

ImportError: cannot import name Article

DONs-iMac:classBased donfox1$ 



Can anyone tell me why I can't import my Article class from my app/models.py.


Lachlan Musicman

unread,
Jul 8, 2014, 10:30:23 PM7/8/14
to django...@googlegroups.com
There is no class Article in any of those blocks. That's why.

Try adding this to models.py this:

class Article(models.Model):
name = models.CharField(max_length=25)
body = models.TextField()
author = models.ForeignKey(Author)

That should help.

cheers
L.
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/d0812a2c-ef08-4615-8255-7657ad6c49e5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
The idea is that a beautiful image is frameable. Everything you need
to see is there: It’s everything you want, and it’s very pleasing
because there’s no extra information that you don’t get to see.
Everything’s in a nice package for you. But sublime art is
unframeable: It’s an image or idea that implies that there’s a bigger
image or idea that you can’t see: You’re only getting to look at a
fraction of it, and in that way it’s both beautiful and scary, because
it’s reminding you that there’s more that you don’t have access to.
It’s now sort of left the piece itself and it’s become your own
invention, so it’s personal as well as being scary as well as being
beautiful, which is what I really like about art like that.
-----------------------------------------------------------------------------------------------------------
Adventure Time http://theholenearthecenteroftheworld.com/

Lachlan Musicman

unread,
Jul 8, 2014, 10:33:05 PM7/8/14
to django...@googlegroups.com

Don Fox

unread,
Jul 8, 2014, 11:19:04 PM7/8/14
to django...@googlegroups.com
I should have mentioned that I already had this method in the models.py:

class Article (models.Model):
    pub_date = models.DateField()
    headline = models.CharField(max_length = 30)
    content  = models.CharField(max_length=500)
    reporter = models.CharField(max_length=20)

This was assumed to be needed after the definition of the article form in the shell. 
As I said I have that same class ArticleForm in the forms.py

Thanks for the reply.
 

Lachlan Musicman

unread,
Jul 9, 2014, 12:09:40 AM7/9/14
to django...@googlegroups.com
On 9 July 2014 13:19, Don Fox <foxd...@gmail.com> wrote:
>
>
> I should have mentioned that I already had this method in the models.py:
>
> class Article (models.Model):
> pub_date = models.DateField()
> headline = models.CharField(max_length = 30)
> content = models.CharField(max_length=500)
> reporter = models.CharField(max_length=20)
>
> This was assumed to be needed after the definition of the article form in
> the shell.
> As I said I have that same class ArticleForm in the forms.py
>
> Thanks for the reply.

Do you still have the same issue?

If so I would suggest it's a PATH problem.

In classBased (which is the folder that has manage.py?), run

python manage.py

and then do

from myapp.models import Article

if that fails, it is a PATH problem.

Googling Django PATH has heaps on it, but something like this shows the way:

https://community.webfaction.com/questions/16064/django-module-import-problem


cheers
L.

Don Fox

unread,
Jul 10, 2014, 11:43:07 AM7/10/14
to django...@googlegroups.com
Problem went away after PyCharm indicated that two imports be removed? Not really sure whet the problem was!
Reply all
Reply to author
Forward
0 new messages