我收到这样的错误:
1048, "Column 'id_fleet_id' cannot be null"
这是我的models.py
class Fleet(models.Model):
id_fleet = models.CharField(max_length=6)
date_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.id_fleet
class Track(models.Model):
name_track = models.CharField(max_length=20, null=True, blank=True)
id_fleet = models.ForeignKey(Fleet)
latitude = models.DecimalField(max_digits=16,decimal_places=2, blank=True, null=True)
altitude = models.DecimalField(max_digits=16,decimal_places=2, blank=True, null=True)
length = models.DecimalField(max_digits=16,decimal_places=2, blank=True, null=True)
geom = MultiPointField()
def __str__(self):
return self.name_track
我尝试放id_fleet = models.ForeignKey(Fleet, null=True, blank=True)
但显示错误
'trackform' object has no attribute 'cleaned_data
这是views.py:
def add_track(request):
if request.method == "POST":
form = TrackForm(request.POST, request.FILES)
if form.is_valid():
p = form.save(commit=False)
p.save()
return redirect('map_add.html', pk=p.pk)
else:
form = TrackForm()
return render(request,'map_add.html', {'form': form}, context_instance= RequestContext(request))
这是forms.py:
class TrackForm(forms.ModelForm):
geom = MultiPointField()
class Meta:
model = Track
fields = ('id_fleet','name_track','length', 'geom')
address = TrackForm()
address.geom = {'type': 'MultiPoint', 'coordinates': [0, 0]}
address.save()
你可以帮我解决这个问题吗?
答案 0 :(得分:1)
由于您在id_fleet_id
字段null
参数为False
时设置了null
或未设置时迁移模型,可能会引发此错误,之后您将True
参数的值更改为{ {1}}但此决定未应用于数据库,请在命令下运行:
python manage.py makemigrations
然后运行:
python manage.py migrate
您也可以更改可空签入数据库,打开项目数据库,然后打开your_app_name_track
表进行编辑,然后检查id_fleet_id
的可空字段。