ESS
ESS

Reputation: 370

Django - Use one models primary key in another model

I want to use one model's primary key in another model in django. Here is what I want to do-

class Verse(models.Model): 
    author = models.ForeignKey('auth.User') 
    title = models.CharField(max_length=400) 
    tags = models.ManyToManyField(Tag) 
    votes = models.IntegerField(default=0)

I want to use this model's primary key to another model in order to check if the user has voted.

class User(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    #get verse id
    #get vote value

I want to be able to use the Verse Model, and the 'votes' column if possible.

Upvotes: 0

Views: 1018

Answers (2)

Hetdev
Hetdev

Reputation: 1525

you can use too:

models.ForeignKey('aplicationname.modelname')

Upvotes: 0

Ajay Gupta
Ajay Gupta

Reputation: 1285

Ofcourse you can use it.

class User(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    verse = models.ForeignKey(Verse)

To access votes value you simply have to write:

User.verse.votes

Upvotes: 1

Related Questions