【问题标题】:Django models and foreign keyDjango 模型和外键
【发布时间】:2020-11-13 04:52:59
【问题描述】:

这是我的第一个 django 项目,我似乎仍然对外键的概念有疑问。我正在使用 mysql db,并且我定义了模型的模型(一部分/sn-p):

class Doctors(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=31)
    fachrichtung = models.CharField(max_length=51)
    adresse = models.CharField(max_length=24)
    plz = models.CharField(max_length=6)
    ort = models.CharField(max_length=9)

    def __str__(self):
        return self.name

    class Meta:
        managed = False
        db_table = 'doctors'

class Leistung(models.Model):
    bezeichnung = models.CharField(max_length=77)
    dauer = models.PositiveIntegerField()
    preis = models.FloatField()
    id = models.IntegerField(primary_key=True)
    artikel = models.ManyToManyField(Artikel)

    class Meta:
        managed = False
        db_table = 'leistung'

class Behandlung(models.Model):
    datum = models.DateField()
    re_nr = models.CharField(max_length=12)
    doctor = models.PositiveIntegerField()
    istpreis = models.FloatField()
    leistungs_text = models.CharField(max_length=32)
    leistung = models.ForeignKey('Leistung', models.DO_NOTHING)
    verrechnet_am = models.DateField()

    class Meta:
        managed = False
        db_table = 'behandlung'

class Rechnung(models.Model):
    id =models.IntegerField(primary_key=True)
    doctor = models.ForeignKey('Doctors', models.DO_NOTHING)
    erstellt_am = models.DateField()

    class Meta:
        managed=False
        db_table='rechnung'

当我启动服务器并希望使用以下行生成 Rechnungen 条目时:

date = datetime.today().strftime('%Y-%m-%d')
    
    
#doctorid is a parameter passed to the view method
doc = Doctors.objects.get(id=doctorid)
    
rechnung = Rechnung(doctor=doc.id,erstellt_am=date)
rechnung.save()

我收到以下错误:

(1054, "Unknown column 'doctor_id' in 'field list'")

当我跑步时:

date = datetime.today().strftime('%Y-%m-%d')
        
        
#doctorid is a parameter passed to the view method
doc = Doctors.objects.get(id=doctorid)
        
rechnung = Rechnung(doctor=doc, erstellt_am=date)
rechnung.save()

我得到同样的错误。所以我然后尝试将医生列重命名为 Rechnungen 中的医生 ID。但后来我得到一个错误

Field 'id' expected a number but got <Doctors: AP>.

所以我从模型中删除了 str 方法,但最终出现了同样的错误.. 之后我将代码修改为:

date = datetime.today().strftime('%Y-%m-%d')
            
            
#doctorid is a parameter passed to the view method
doc = Doctors.objects.get(id=doctorid)
            
rechnung = Rechnung(doctor=doc.id, erstellt_am=date)
rechnung.save()

这让我回到了我的第一个错误。而且我有一种绕圈子跑的感觉。我还尝试将医生 ID 传递给Rechnung(doctor=doctorid, erstellt_am=date),但它需要医生实例。所以在删除外键之前,有没有人建议我如何解决这个问题?谢谢!

【问题讨论】:

  • 您是否运行了所有迁移?
  • 你可以试试Rechnung(doctor_id=doctorid, erstellt_am=date)吗?
  • @Vincent 是的 ????????我运行了所有的迁移!
  • @ruddra 我会尝试...

标签: django database model key


【解决方案1】:

实际猜测医生模型中的整数字段不会自动递增以解决此问题,如果可能,请删除数据库中的所有数据,然后您必须在医生模型中编辑以下 id 字段

class Doctors(models.Model):
    id = models.AutoField(primary_key=True)
Or

class Doctors(models.Model):
    id = models.BigAutoField(primary_key=True)

在此应用之后

python manage.py makemigrations 
python manage.py migrate 

【讨论】:

  • ??????这不仅仅是猜测,而是最好的猜测!谢谢!显然,使用不是由 db 生成的 id 是不行的。不得不重置我所有的迁移,重新设计模型,迁移它并根据新模型调整表单、模板、视图???..再次感谢!
  • 这是我的荣幸?
猜你喜欢
  • 1970-01-01
  • 2010-11-10
  • 2013-01-17
  • 2013-05-27
  • 1970-01-01
  • 2019-07-19
  • 2015-01-29
  • 2018-01-10
  • 2011-11-23
相关资源
最近更新 更多