gpt4 book ai didi

python - Django/mySQL - 属性错误 : 'ForeignKey' object has no attribute 'IntegerField'

转载 作者:行者123 更新时间:2023-11-28 22:34:50 25 4
gpt4 key购买 nike

我有一个在 Windows 上运行良好的 Django 项目。我正在尝试将其移动到 ubuntu。当我运行 python manage.py runserver 8000

时存在一些问题

File "/home/zhaojf1/Web-Interaction-APP/fileUpload_app/models.py", line 151, in Machine number_pins = models.IntegerField(blank=True, null=True)

AttributeError: 'ForeignKey' object has no attribute 'IntegerField'

此外,Machine 表中的这一列不是外键项。

views.py 中的代码:

140 class Machine(models.Model):
141 model = models.ForeignKey('Model', db_column='model', blank=True, null=True)
142 sn = models.CharField(max_length=50, blank=True)
143 mine_lon = models.CharField(max_length=50, blank=True)
144 mine_lat = models.CharField(max_length=50, blank=True)
145 location = models.CharField(max_length=50, blank=True)
146 total_hours = models.IntegerField(blank=True, null=True)
147 travel_hours = models.IntegerField(blank=True, null=True)
148 machine_id = models.IntegerField(primary_key=True)
149 models = models.ForeignKey('Model', db_column='models', blank=True, null=True)
150 # photo_set_num = models.IntegerField(blank=True, null=True)
151 number_pins = models.IntegerField(blank=True, null=True)
152 class Meta:
153 managed = False
154 db_table = 'machine'

我有一个 mysql 数据库,我使用

直接从 mysql 生成 models.py

$ python manage.py inspectdb > models.py

最佳答案

您的名为 models 的字段隐藏了从 Django 导入的 models。您可以重命名该字段:

other_name_for_models = models.ForeignKey('Model', db_column='models', blank=True, null=True)

或者用不同的名字导入模块

from django.db import models as django_models

class Machine(django_models.Model):
models = django_models.ForeignKey('Model', db_column='model', blank=True, null=True)

关于python - Django/mySQL - 属性错误 : 'ForeignKey' object has no attribute 'IntegerField' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com