Reputation: 370
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
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