Ahmed
Ahmed

Reputation: 97

How to access another app model in django?

I have two apps main and table in my main I have a model UserSelect and in table is a model Bowler I need to do this

from main.models import UserSelect, User

class Bowlers(models.Model):
 users = models.ManyToManyField(User, through='UserSelect')

but it gives error that "Field specifies a many-to-many relation through model 'UserSelect', which has not been installed" so how can I do this?

Upvotes: 0

Views: 418

Answers (1)

Iain Shelvington
Iain Shelvington

Reputation: 32294

You can have lazy references to models from any app by referencing it with a string

class Bowlers(models.Model):
    users = models.ManyToManyField('main.User', through='main.UserSelect')

But the specific issue you are having is that you should pass the UserSelect class as the through argument not as a string

class Bowlers(models.Model):
    users = models.ManyToManyField(User, through=UserSelect)

Upvotes: 1

Related Questions