Patryk Perduta
Patryk Perduta

Reputation: 416

How to query database about Model from another App?

I have models like this:

class Subscription(models.Model):
    visable_name = models.CharField(max_length=50, unique=True)

    recipe_name = models.CharField(max_length=50)
    website_url = models.URLField()


class User(models.Model):
    username = models.CharField(max_length=50)


class UserSubs(models.Model):
    subscription = models.ForeignKey(Subscription, to_field='visable_name')
    user = models.ForeignKey(User, to_field='username')

And I want to prepare simple ranking, so I came up with something like this: Subscription.objects.annotate(total=models.Count('usersubs')).order_by('-total')

The true problem is that I just discovered that my "simple ranking" should be in another App, where I can't even do from FirstApp import models.Subscription, becouse I get ImportError: cannot import name Subscription.

I actually have no idea how it has to be done.. Maybe should I give up from separate these two Apps?

Upvotes: 1

Views: 370

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599470

I still don't really understand why you are trying to split these up, but it seems likely you have a circular dependency: your model files are both trying to import each other. You probably need to remove the import from one side: note that if you're importing simply to use as a reference in defining a ForeignKey, you can use a string: models.ForeignKey('FirstApp.Subscription') instead of the actual class.

Upvotes: 1

Related Questions