I got it! It turns out I was right in my suspicion. It doesn’t look like you
can use the string notation to reference a model in a different module. Is
that expected? Is there any way to do that?



Basically, I simply moved the RestaurantHoodMap into the hood.py file,
changed the string name to “RestaurantHoodMap”, and viola, it works.



Can anyone explain why the string notation won’t work for a model in a
different module?



Thanks!



-Jamie



*From:* James DeMichele [mailto:james.demich...@redfin.com]
*Sent:* Thursday, September 29, 2011 12:34 PM
*To:* django-users@googlegroups.com
*Subject:* Re: ManyToMany problem



Thanks, Tom. So, I've changed my code slightly.....but I'm still getting an
error. I'm pretty much following exactly what is in the Django docs as well.
I'm wondering if it's because I have my models in different modules. That
shouldn't matter though should it? Maybe I'm doing something wrong with the
string syntax. More help would be appreciated.



Updated files:



*models/hoods.py*

<import Restaurant>

class Hood(AbstractDateModel):

    name = models.CharField(unique=True)

    restaurants=models.ManyToManyField(Restaurant,
through='restaurant_hood_map.RestaurantHoodMap')



    class Meta:

        db_table = "hoods"

        managed=False

        app_label="delivery"



*models/restaurants.py*

class Restaurant(AbstractDateModel):

    name=models.ForeignKey(CompanyName)



    class Meta:

        db_table="restaurants"

        managed=False

        app_label="delivery"



*models/restaurant_hood_map.py*

<import Restaurant and Hood>

class RestaurantHoodMap(models.Model):

    restaurant = models.ForeignKey(Restaurant)

    hood = models.ForeignKey(Hood)



    class Meta:

        db_table="restaurant_hood_map"

        managed=False

        app_label="delivery"



.....Ok, so that's exactly what is in the Django Doc:
https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manytomany



However, here's the error I am getting this thing at the command line:



>>> from models.restaurant_hood_map import RestaurantHoodMap

>>> RestaurantHoodMap.objects.all()

[<RestaurantHoodMap: RestaurantHoodMap object>]

>>> RestaurantHoodMap.objects.all()[0]

<RestaurantHoodMap: RestaurantHoodMap object>

>>> RestaurantHoodMap.objects.all()[0].hood

<Hood: Hood object>

>>> RestaurantHoodMap.objects.all()[0].hood.name

u'Westlands'

>>> RestaurantHoodMap.objects.all()[0].hood.restaurants

Traceback (most recent call last):

  File "<console>", line 1, in <module>

  File "C:\Python\lib\site-packages\django\db\models\fields\related.py",
line 722, in __get__

    RelatedManager = create_many_related_manager(superclass, self.field.rel)

  File "C:\Python\lib\site-packages\django\db\models\fields\related.py",
line 478, in create_many_related_manager

    class ManyRelatedManager(superclass):

  File "C:\Python\lib\site-packages\django\db\models\fields\related.py",
line 501, in ManyRelatedManager

    if rel.through._meta.auto_created:

AttributeError: 'str' object has no attribute '_meta'



Please help if you can!



Thanks.



-Jamie



On Thu, Sep 29, 2011 at 11:38 AM, Tom Evans <tevans...@googlemail.com>
wrote:

On Thu, Sep 29, 2011 at 5:20 PM, James DeMichele
<james.demich...@redfin.com> wrote:
> Thanks for the response, that's a completely inaccurate statement.
>
> Here's an example from the Django docs:
>
> https://docs.djangoproject.com/en/1.3/topics/db/models/#intermediary-manyt
> omany
>
> The Group object has through="Membership".
>
> Although, one thing I notice is that it looks like in the Django example
> it is only using 1 ManyToMany field (e.g. the Person model does not have a
> manytomany field). Is that required? Only one of the related items can
> have a ManyToMany declared?
>
> That wouldn't make a lot of sense though, right? I would want to ask for
> all Restaurants a Hood as....and vice versa. I'd want all of the Hoods
> that belong to a Restaurant.
>

You only place it on one side of the relationship. Django magic adds
the appropriate methods to the other side of the relationship. DRY -
Dont Repeat Yourself.

Cheers

Tom


--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to