我无法在同一个字段中添加两个外键约束?
我对这段关系并不是100%肯定,所以我已经把这些表格包括在内了。希望它也可以帮助其他人学习Django: 的交易
+----------+-----+---------------------+
| guid | plu | datetime |
+----------+-----+---------------------+
| 00003516 | 1 | 2015-09-22 12:11:12 |
| 0000386a | 2 | 2015-02-22 12:11:10 |
| 0000c59d | 2 | 2015-03-22 12:11:10 |
| 0000f03f | 3 | 2015-01-22 12:12:12 |
+----------+-----+---------------------+
PLU
+-----+------+
| plu | name |
+-----+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
+-----+------+
金融
+-----+------------+------------+-------+
| plu | from | to | price |
+-----+------------+------------+-------+
| 1 | 2013-01-22 | 2015-01-23 | 10 |
| 1 | 2015-01-23 | 2015-02-29 | 20 |
| 2 | 2013-01-22 | 2015-01-22 | 10 |
| 3 | 2013-01-22 | 2015-01-22 | 30 |
+-----+------------+------------+-------+
基于此,我创建了以下模型: 金融
class Finance(models.Model):
guid = models.AutoField(primary_key=True)
from = models.DateField()
to = models.DateField()
price = models.DecimalField(max_digits=10, decimal_places=2)
PLU
class Plu(models.Model):
plu = models.IntegerField(primary_key=True)
name = models.CharField(max_length=200, null=True)
交易
class Transaction(models.Model):
guid = models.CharField(primary_key=True, max_length=38)
plu = models.ForeignKey(Plu, db_column='plu')
finance = models.ForeignKey(Finance, db_column='plu')
datetime = models.DateTimeField()
当我尝试运行此操作时,我收到以下错误 - 该怎么办?:
app.Transaction: (models.E007) Field 'finance' has column name 'plu' that is used by another field.
HINT: Specify a 'db_column' for the field.
答案 0 :(得分:1)
您已复制并粘贴并包含相同的db_column名称,您需要更改它
finance = models.ForeignKey(Finance)
注意:您根本不需要指定db_column,因为您只需将其设置为字段名称
String