Totty.js
Totty.js

Reputation: 15841

Circular import in python+django?! how to make it work?

Hello I'm spliting my files because the model is getting bigger. So here we are again with problems:

My models; If in my Category model I remove the "ArticleToCategory" and the many-to-many relationship it works well. But I need them!

How to fix it?

I deleted the model.py in order to load files from the model package.

Category (models.category):

class Category(MPTTModel):
    # relationships
    from RubeteDjango01.generic.models.article import Article
    from RubeteDjango01.generic.models.article_to_category import ArticleToCategory
    articles = m.ManyToManyField(Article, through=ArticleToCategory)

ArticleToCategory (models.article_to_category):

from django.db import models as m

class ArticleToCategory(m.Model):
    from RubeteDjango01.generic.models.article import Article
    from RubeteDjango01.generic.models.category import Category

    article = m.ForeignKey(Article)
    category = m.ForeignKey(Category)

    class Meta:
        db_table = 'articles_to_categories'
        verbose_name_plural = 'ArticlesToCategories'

thanks

Upvotes: 2

Views: 236

Answers (1)

second
second

Reputation: 28637

You can define foreign keys using strings, to avoid exactly this problem.

class Art2C(..):
    art = m.ForeignKey('Article')
    from_other_app = m.ForeignKey('other_app.Article')

Upvotes: 7

Related Questions