我有贸易模型/ 贸易 .py:
from datetime import datetime
from django.db import models
from home.models import Player
class Trade(models.Model):
buyer = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='buyer'),
buyee = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='buyee'),
date = models.DateTimeField(default=datetime.now)
模型首页/ 播放器 .py:
from django.contrib.auth.models import User
from django.db import models
class Player(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='user')
value = models.IntegerField(default=1500)
owner = models.OneToOneField(User, on_delete=models.CASCADE, related_name='owner', blank=True, null=True)
交易发生在2个玩家之间。也就是说,交易中的买方和买方字段是玩家的外键。
现在,当我为 Trade 模型进行迁移时,这就是我得到的:
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Trade',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateTimeField(default=datetime.datetime.now)),
],
),
]
为什么迁移中缺少2个外键字段?
答案 0 :(得分:2)
这是由于Trade
类中每个字段末尾的逗号引起的。删除逗号,它将起作用。
class Trade(models.Model):
buyer = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='buyer')
buyee = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='buyee')
date = models.DateTimeField(default=datetime.now)