If I want to add a Foreignkey to a model from another class in the same model e.g.
class1 (models.Model):
variable1 = models.IntegerField()
class2 (models.Model):
variable2 = models.CharField()
foreignkey = models.Foreignkey(class1.variable1)
Is that possible? Does that make sense as a programming move?
This ForeignKey would be an ID Number (like a primary key) that I would like to import to other classes as well.
@ Manoj Govindan:
e.g.
class author(models.Model):
authorlabel= models.IntegerField() # With choices
...
class books(models.Model):
books=models.CharField()
f开发者_运维百科oreignkey= models.Foreignkey(author.authorlabel)
So that I have that data available in that table(?)/model as well.
Thanks!
Perhaps this is what you are looking for:
models.ForeignKey(class1, to_field = 'variable1')
Relevant documentation is here.
This ForeignKey would be an ID Number (like a primary key) that I would like to import to other classes as well.
Not sure what you mean by this. Can you rephrase it and add an example?
Addition to Manoj Govindan's answer...
This ForeignKey would be an ID Number (like a primary key) that I would like to import to other classes as well.
No it do not have to be a number, it can be a string, a datetime value or something else... But it have to be unique.
精彩评论