我一直在阅读与我类似的问题,但它们对我没有帮助。
我在尝试提交表单时收到此错误:
Cannot assign "9": "Characterweapons.weaponid" must be a "Weapons" instance.
这个"9"是我从表单中得到的武器的ID,所以这非常好,但是当我尝试将其放在Characterweapons表的weaponid列中时,它给了我错误。
models.py:
from __future__ import unicode_literals
from django.db import models
class Category(models.Model):
categoryid = models.AutoField(db_column='CategoryID', primary_key=True) # Field name made lowercase.
categoryname = models.CharField(db_column='CategoryName', max_length=50, blank=True, null=True) # Field name made lowercase.
class Meta:
managed = True
db_table = 'category'
def __str__(self):
return self.categoryname
class Characters(models.Model):
characterid = models.AutoField(db_column='CharacterID', primary_key=True) # Field name made lowercase.
name = models.CharField(db_column='Name', unique=True, max_length=255) # Field name made lowercase.
level = models.IntegerField(db_column='Level') # Field name made lowercase.
credits = models.IntegerField(db_column='Credits') # Field name made lowercase.
class Meta:
managed = True
db_table = 'characters'
def __str__(self):
return '%s %s %s' % (self.name, self.level, self.credits)
class Weapons(models.Model):
weaponid = models.AutoField(db_column='WeaponID', primary_key=True) # Field name made lowercase.
weaponname = models.CharField(db_column='WeaponName', unique=True, max_length=255) # Field name made lowercase.
class Meta:
managed = True
db_table = 'weapons'
def __str__(self):
return '%s %r' % (self.weaponname, self.weaponid)
class Characterweapons(models.Model):
characterid = models.ForeignKey(Characters, models.DO_NOTHING, db_column='CharacterID') # Field name made lowercase.
weaponid = models.ForeignKey(Weapons, models.DO_NOTHING, db_column='WeaponID', blank=True, null=True) # Field name made lowercase.
categoryid = models.ForeignKey(Category, models.DO_NOTHING, db_column='CategoryID', blank=True, null=True) # Field name made lowercase.
quantity = models.IntegerField(db_column='Quantity', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = True
db_table = 'characterweapons'
def __str__(self):
return '%s' % (self.quantity)
class DjangoMigrations(models.Model):
app = models.CharField(max_length=255)
name = models.CharField(max_length=255)
applied = models.DateTimeField()
class Meta:
managed = True
db_table = 'django_migrations'
我不知道这是否是FK问题,PK...不知道,我找不到解决它的信息...
views.py:(错误必须在哪里(:
def submission(request):
print("Registered successfully")
Name = request.POST["Name"]
Level = request.POST["Level"]
Credits = request.POST["Credits"]
Mainhand = request.POST["Mainhand"]
Offhand = request.POST["Offhand"]
info = Characters(name=Name,level=Level,credits=Credits)
info.save()
mh=
infomh = Weapons.objects.values_list('weaponid',flat=True)
a=0;
for a in infomh:
if a == Mainhand:
a = Mainhand;
print("a: ")
print(a)
print("Mainhand")
print(Mainhand)
print("infmh:")
print(infomh)
charid = Characters.objects.latest('characterid')
info_mh = Characterweapons(characterid=charid,categoryid=1,weaponid=a)
info_mh.save()
#info_oh = Characterweapons(characterid=charid,weaponid=9,categoryid=2)
#info_oh.save()
return return_charnames(request)
通过这些打印,我确保我的 武器 ID 的值是正确的,它们是。 我还想指出,"武器 id"列中的值=9存在于"武器"表中。 谢谢大家。
我认为你最好先修复你的模型。在 Django 中,ForeignKey
在概念上是对你引用的对象的引用,而不是 id。当然,在数据库中,它存储为id,但这是一个你不应该太关心的层。
所以我建议首先将weaponid
等字段重命名为weapon
:
class Characterweapons(models.Model):
character= models.ForeignKey(Characters, models.DO_NOTHING, db_column='CharacterID')
weapon= models.ForeignKey(Weapons, models.DO_NOTHING, db_column='WeaponID', blank=True, null=True)
category= models.ForeignKey(Category, models.DO_NOTHING, db_column='CategoryID', blank=True, null=True)
quantity = models.IntegerField(db_column='Quantity', blank=True, null=True)
# ...
现在,如果你构造一个名为fieldname
的ForeignKey
,Django 实际上引入了两个字段:fieldname
,如前所述,它是对ForeignKey
引用的模型对象的引用,以及存储该对象的id
(主键(的字段fieldname_id
。因此,这两个领域就像"双胞胎"。
然后,我们可以重写submission
视图,如下所示:
def submission(request):
print("Registered successfully")
name = request.POST["Name"]
level = request.POST["Level"]
credits = request.POST["Credits"]
mainhand = request.POST["Mainhand"]
offhand = request.POST["Offhand"]
info = Characters.objects.create(name=name, level=level,credits=credits)
if Weapons.objects.filter(weapon_id=mainhand).exists():
a = mainhand
info_mh = Characterweapons.objects.create(
character_id=info.pk,
category_id=1,
weapon_id=a
)
return return_charnames(request)
在上面,我也做了一些重构:
- 在 Python 变量中,函数通常具有小写名称;
- 您不需要遍历
weapon_id
,您可以使用EXISTS
查询,这通常更快; - 您可以改用
Model.objects.create(..)
,而不是先构造模型实例,然后对其进行obj.save()
; - 您可能不想获取
latest(..)
值,而是改用info
对象的pk
,因为这是我们构造的那个,此外,如果没有定义顺序(如此处(,则顺序可以是数据库喜欢的任何顺序,而不是本身最新添加的顺序。
您可能仍然需要重写硬编码标识符(如a=1
和category_id=1
,因为这些对象完全可能不存在于数据库中(。
你的外键 武器 模型角色武器中的ID不仅是它的id,也是武器实例。 从该 id 查询对象并分配它。