Quantcast

Cannot get Many to Many Relationship To work - "name" not defined error message

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Cannot get Many to Many Relationship To work - "name" not defined error message

MerMer

.
I am trying to create a many to many relationship between two classes
(Promotion and Category).

When I add "categories = models.ManyToManyField(Category)" to the
Promotion class I get an errror saying "Category is not defined".

However, when  I add "promotions = models.ManyToManyField(Promotion)"
to the Category Class it works fine!

This is driving me crazy, as a newbie it's not easy to diagnose where
the problem lies.  I've been very impressed with the documentation,
review etc of Django - but after 24 hours of not getting too far, I'm
getting rather frustrated.

MerMer


from django.db import models
import datetime

class Promotion (models.Model):
    promo_headline = models.CharField(maxlength=400, core=True)
    promo_sbody = models.TextField(help_text="This is for a short
description about the offer")
    promo_lbody = models.TextField(help_text="This if sfor a long
description about the offer")
    pub_date = models.DateTimeField()
    categories = models.ManyToManyField(Category)
    start_date = models.DateTimeField(core=True)
    expiry_date = models.DateTimeField()
    promoter_name = models.ForeignKey(Promoter,
edit_inline=models.TABULAR)
    is_multi_takeup = models.BooleanField(help_text="Tick if the offer
can be taken up by the same person more than once")
    takeup_url = models.URLField(help_text="URL where the offer will be
taken up")
    question_email = models.EmailField(core=True, help_text="where
questions about this offer will be sent")
    terms_conditions = models.TextField(core=True)
    def __str__(self):
        return self.promo_headline

class Category (models.Model):
    category_name = models.CharField(maxlength=400)
    def __str__(self):
        return self.category_name
    class Admin:
        pass


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cannot get Many to Many Relationship To work - "name" not defined error message

Rajesh Dhawan

You are seeing that error because Category is defined after Promotion
in your model file. In such cases, you can use string lookup instead a
class lookup, like this:

categories = models.ManyToManyField('Category')

Note the Category in quotes instead of without quotes.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cannot get Many to Many Relationship To work - "name" not defined error message

Jay Parlar-2
In reply to this post by MerMer

On 10/4/06, MerMer <[hidden email]> wrote:

>
> .
> I am trying to create a many to many relationship between two classes
> (Promotion and Category).
>
> When I add "categories = models.ManyToManyField(Category)" to the
> Promotion class I get an errror saying "Category is not defined".
>
> However, when  I add "promotions = models.ManyToManyField(Promotion)"
> to the Category Class it works fine!
>
> This is driving me crazy, as a newbie it's not easy to diagnose where
> the problem lies.  I've been very impressed with the documentation,
> review etc of Django - but after 24 hours of not getting too far, I'm
> getting rather frustrated.
>
> MerMer
>
>
> from django.db import models
> import datetime
>
> class Promotion (models.Model):
>     promo_headline = models.CharField(maxlength=400, core=True)
>     promo_sbody = models.TextField(help_text="This is for a short
> description about the offer")
>     promo_lbody = models.TextField(help_text="This if sfor a long
> description about the offer")
>     pub_date = models.DateTimeField()
>     categories = models.ManyToManyField(Category)
>     start_date = models.DateTimeField(core=True)
>     expiry_date = models.DateTimeField()
>     promoter_name = models.ForeignKey(Promoter,
> edit_inline=models.TABULAR)
>     is_multi_takeup = models.BooleanField(help_text="Tick if the offer
> can be taken up by the same person more than once")
>     takeup_url = models.URLField(help_text="URL where the offer will be
> taken up")
>     question_email = models.EmailField(core=True, help_text="where
> questions about this offer will be sent")
>     terms_conditions = models.TextField(core=True)
>     def __str__(self):
>         return self.promo_headline
>
> class Category (models.Model):
>     category_name = models.CharField(maxlength=400)
>     def __str__(self):
>         return self.category_name
>     class Admin:
>         pass
>

The reason the first was didn't work, but the second did, is because
of order in the file. A model has to already be declared before you
can make reference to it. So when you wrote

categories = models.ManyToManyField(Category)

in the Promotion class, you get an error because 'Category' is not
defined yet. The simple solution is to put your Promotion class
*after* the Category class.

Jay P.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cannot get Many to Many Relationship To work - "name" not defined error message

MerMer
In reply to this post by Rajesh Dhawan

Thanks Jay and Rajesh,

I've  just discovered this myself by switching the code around.  This
is a real GOTCHA and not immediately obvious for a newbie like me.  I
can't see anything in the documentation that mentions this - so I
presumed it was a bug.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cannot get Many to Many Relationship To work - "name" not defined error message

Rajesh Dhawan

Just for posterity, I would like to say that this is indeed mentioned
in the documentation here:

http://www.djangoproject.com/documentation/model_api/#many-to-one-relationships

Excerpt: "If you need to create a relationship on a model that has not
yet been defined, you can use the name of the model, rather than the
model object itself:"


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cannot get Many to Many Relationship To work - "name" not defined error message

Jay Parlar-2
In reply to this post by MerMer

On 10/4/06, MerMer <[hidden email]> wrote:
>
> Thanks Jay and Rajesh,
>
> I've  just discovered this myself by switching the code around.  This
> is a real GOTCHA and not immediately obvious for a newbie like me.  I
> can't see anything in the documentation that mentions this - so I
> presumed it was a bug.

Well, it's a Python thing, really. To use a class, that class has to be defined.

And it *is* in the Django documentation:

http://www.djangoproject.com/documentation/model_api/#many-to-many-relationships

"...and you can refer to as-yet undefined models by using a string
containing the model name."

The Django documentation tends to deal more with the case of reading
it before you dive in, as opposed to using it to debug tracebacks.

Jay P.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Cannot get Many to Many Relationship To work - "name" not defined error message

MerMer

Jay,  I did read the documentation before jumping in - but it's not all
necessarily clear for someone who hasn't alot of experience in Python
of Django.

Because of my unfamiliarity I had presumed that all the classes in a
single Models.py were immediately visible to each other.   There is
nothing specific in the link you provided to make this explicit in
relation to a ManyToMany relationship.   However, having read Rajesh's
link I can now see that there is a good example of using a string and
that gives a strong hint.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Re: Cannot get Many to Many Relationship To work - "name" not defined error message

James Bennett

On 10/3/06, MerMer <[hidden email]> wrote:
> Jay,  I did read the documentation before jumping in - but it's not all
> necessarily clear for someone who hasn't alot of experience in Python
> of Django.

This is a tough problem to deal with, though; at some point we have to
be able to assume a certain level of familiarity with Python or
there's no way we can effectively document Django; we'll spend more
time documenting Python, which would be duplication of the already
pretty-darn-good official Python documentation.

I think maybe one thing we could do is build up a list of Python
tutorials and docs that we recommend people work through, either
before they start playing with Django or as they go.

--
"May the forces of evil become confused on the way to your house."
  -- George Carlin

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Mile-high view of Django (was "Re: Cannot get Many to Many Relationship To work...")

Tim Chase-7
In reply to this post by MerMer

>> Jay,  I did read the documentation before jumping in - but
>> it's not all necessarily clear for someone who hasn't alot
>> of experience in Python of Django.

Are there any good resources where one can find the mile-high
overview of Django's inner workings?  An "executive summary", if
you will.  Some place where one can get a feel for how templates,
manipulators, object-models, ORM, server interface (whether
self-run or attached via mod_python/fastcgi), etc all tie together.

I keep learning little bits here and there as I encounter them,
but am having trouble fitting all the pieces together into a
cohesive big-picture.  Like the person to whose post I'm
responding, I can read the docs, but extracting the big-picutre
(which in turn would help in knowing where to look in the docs)
can be tough.

Thanks,

-tkc





--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Mile-high view of Django (was "Re: Cannot get Many to Many Relationship To work...")

MerMer



> I keep learning little bits here and there as I encounter them,
> but am having trouble fitting all the pieces together into a
> cohesive big-picture.  Like the person to whose post I'm
> responding, I can read the docs, but extracting the big-picutre
> (which in turn would help in knowing where to look in the docs)
> can be tough.
>

Thats' interesting to hear.  As a newbie I'm having a different
experience.  I think the documentation, as a whole, does an excellent
job at giving the bigger picture (though I can see the argument for an
shorter, white document to save).

Where I'm constantly coming unstuck is on the detail.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Mile-high view of Django (was "Re: Cannot get Many to Many Relationship To work...")

Guillermo Fernandez Castellanos
In reply to this post by Tim Chase-7

Hi,

This might help:
http://simon.incutio.com/archive/2005/08/15/request

Enjoy,

G

On 10/3/06, Tim Chase <[hidden email]> wrote:

>
> >> Jay,  I did read the documentation before jumping in - but
> >> it's not all necessarily clear for someone who hasn't alot
> >> of experience in Python of Django.
>
> Are there any good resources where one can find the mile-high
> overview of Django's inner workings?  An "executive summary", if
> you will.  Some place where one can get a feel for how templates,
> manipulators, object-models, ORM, server interface (whether
> self-run or attached via mod_python/fastcgi), etc all tie together.
>
> I keep learning little bits here and there as I encounter them,
> but am having trouble fitting all the pieces together into a
> cohesive big-picture.  Like the person to whose post I'm
> responding, I can read the docs, but extracting the big-picutre
> (which in turn would help in knowing where to look in the docs)
> can be tough.
>
> Thanks,
>
> -tkc
>
>
>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Re: Mile-high view of Django (was "Re: Cannot get Many to Many Relationship To work...")

James Bennett

On 10/3/06, Guillermo Fernandez Castellanos
<[hidden email]> wrote:
> This might help:
> http://simon.incutio.com/archive/2005/08/15/request

For the detail-oriented folks, I also maintain a more in-depth version:

http://www.b-list.org/weblog/2006/06/13/how-django-processes-request

--
"May the forces of evil become confused on the way to your house."
  -- George Carlin

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Mile-high view of Django

Tim Chase-7

>> http://simon.incutio.com/archive/2005/08/15/request
>
> For the detail-oriented folks, I also maintain a more in-depth version:
>
> http://www.b-list.org/weblog/2006/06/13/how-django-processes-request

Yes, these were exactly the sort of thing I was hunting
for--particularly the detailed process-flow described in the
second link helps pull together those bits that have been laying
around disorganized in my head.

Thanks!

-tkc





--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Re: Cannot get Many to Many Relationship To work - "name" not defined error message

Waylan Limberg
In reply to this post by James Bennett

On 10/3/06, James Bennett <[hidden email]> wrote:
>
> I think maybe one thing we could do is build up a list of Python
> tutorials and docs that we recommend people work through, either
> before they start playing with Django or as they go.
>

+1

This seems to come up from time to time on the list. Of course, this
page [1] already provides a decent list, but perhaps a list more
specific to the skills needed for Django would be helpful as well.
Although, is there really any difference in the skills needed? Maybe
not.

[1]: http://www.python.org/doc/

--
----
Waylan Limberg
[hidden email]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Loading...