I am very new to Django. I run into a problem where I need to refer a column that is neither a primary key nor unique value from another table. Here is my code:
class Assignment(models.Model):
assignment_id = models.CharField(primary_key=True, max_length=11)
investor_id = models.ForeignKey('Investor', to_field='investor_id', models.DO_NOTHING)
issuer_id = models.ForeignKey('Issuer', to_field='issuer_id', models.DO_NOTHING)
investor_risk_attitude = models.ForeignKey('Investor', to_field='investor_risk_attitude', models.Do_NOTHING)
interest_rate = models.FloatField(blank=True, null=True)
tranche = models.CharField(max_length=20, blank=True, null=True)
def get_absolute_url(self):
return reverse ('assignment:index')
class Meta:
managed = False
db_table = 'assignment'
def __str__(self):
return self.assignment_id
class Investor(models.Model):
investor_id = models.CharField(primary_key=True, max_length=11)
investor_name = models.CharField(max_length=45, blank=True, null=True)
investor_risk_attitude = models.CharField(max_length=45, blank=True, null=True,db_index=True)
def get_absolute_url(self):
return reverse ('investor:index')
class Meta:
managed = False
db_table = 'investor'
def __str__(self):
return self.investor_id
class Issuer(models.Model):
issuer_id = models.CharField(primary_key=True, max_length=11)
issuer_name = models.CharField(max_length=45, blank=True, null=True)
issuer_category = models.CharField(max_length=45, blank=True, null=True)
total_pool_amount = models.FloatField(blank=True, null=True)
login_id = models.CharField(max_length=45, blank=True, null=True)
login_password = models.CharField(max_length=45, blank=True, null=True)
class Meta:
managed = False
db_table = 'issuer'
def __str__(self):
return self.issuer_id
In Assignment table, I need to refer investor_risk_attitude from the Investor table, which I have made an index, but it is not a unique value. Because investor_id is also a foreign key in Assignment table, so I need the risk_attitude to match with its investor_id here.
I also have a form that allows users to add new Assignment record to the table, the code is as below.
class AddAssignment(CreateView):
model = Assignment
fields = ['assignment_id', 'investor', 'issuer', 'investor_risk_attitude', 'interest_rate', 'tranche']
Is there any way I can execute this in a way where the corresponding risk_attitude would pop up once the user chooses the investor in the drop-down menu in the form? Thank you very much?