Wednesday, July 31, 2013

Re: many-to-many between apps

On 31/07/2013 5:08pm, Mike Dewhirst wrote:
> Is it possible to establish a many-to-many relationship between a model
> in one app and a model in another app?

Yes. I needed to avoid importing the class and use ...

class Region(models.Model):
... various fields ...
ref = models.ManyToManyField('item.Reference')

Lovely!

Thanks for Django

>
> I've just tried to do it and run into what I think is circular import
> trouble. This is a retro-fit after initial deployment.
>
> #in item.models.reference
> class Reference(models.Model):
> ... various fields ...
> class Meta:
> app_label = 'item'
>
> #in item.models.reference_region
> from company import Region
>
> class Reference_Region(models.Model):
> region = models.ForeignKey(Region)
> reference = models.ForeignKey(Reference)
> class Meta:
> app_label = 'item'
>
>
> #in company.models.region
> from item import Reference
>
> class Region(models.Model):
> ... various fields ...
> ref = models.ManyToManyField(Reference)
>
> I have tried it the other way declaring the many-to-many field in
> Reference and the same thing happens.
>
> ImportError: cannot import name Reference
> and
> ImportError: cannot import name Region
> respectively
>
> Any ideas appreciated
>
> Thanks
>
> Mike
>

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment