我使用ogr2ogr成功将我的Geojson数据导入PostGIS。
我运行以下命令:python manage.py inspectdb
class Route(models.Model):
ogc_fid = models.AutoField(primary_key=True)
distance = models.FloatField(blank=True, null=True)
route_type = models.CharField(max_length=-1, blank=True, null=True)
route_long = models.CharField(max_length=-1, blank=True, null=True)
route_name = models.CharField(max_length=-1, blank=True, null=True)
agency_id = models.ForeignKey(max_length=-1, blank=True, null=True)
route_id = models.CharField(max_length=-1, blank=True, null=True)
route_url = models.CharField(max_length=-1, blank=True, null=True)
route_desc = models.CharField(max_length=500, blank=True, null=True)
duration = models.FloatField(blank=True, null=True)
shape_id = models.IntegerField(blank=True, null=True)
route_tcolor = models.CharField(max_length=-1, blank=True, null=True)
route_color = models.CharField(max_length=-1, blank=True, null=True)
wkb_geometry = models.LineStringField(blank=True, null=True)
class Meta:
managed = False
db_table = 'network_route'
当然,我必须将max_length
修改为正整数,并将manage
设置为True
,所以我做了。
我还添加了一个额外的列created_at
和updated_at
。
产生以下最终模型:
class Route(models.Model):
ogc_fid = models.AutoField(primary_key=True)
distance = models.FloatField(blank=True, null=True)
route_type = models.CharField(max_length=100, blank=True, null=True)
route_long = models.CharField(max_length=200, blank=True, null=True)
route_name = models.CharField(max_length=100, blank=True, null=True)
agency_id = models.ForeignKey(Agency, on_delete=models.CASCADE)
route_id = models.CharField(max_length=100, blank=True, null=True)
route_url = models.CharField(max_length=300, blank=True, null=True)
route_desc = models.CharField(max_length=500, blank=True, null=True)
duration = models.FloatField(blank=True, null=True)
shape_id = models.IntegerField(blank=True, null=True)
route_tcolor = models.CharField(max_length=100, blank=True, null=True)
route_color = models.CharField(max_length=100, blank=True, null=True)
wkb_geometry = models.LineStringField(blank=True, null=True)
created_at = models.DateTimeField(default=timezone.now(),editable=False)
updated_at = models.DateTimeField(blank=True, null=True)
objects = models.GeoManager()
class Meta:
managed = True
db_table = 'network_route'
def save(self, *args, **kwargs):
#On save, update timestamps
if not self.id:
self.created_at = timezone.now()
self.updated_at = timezone.now()
return super(Route, self).save(*args, **kwargs)
def __str__(self):
return self.route_id
我运行:makemigrations [appname]
,它只检测Meta
- Change Meta options on route
如何在模型中添加其他更改,例如迁移所捕获的created_at
和updated_at
?
我做错了吗?
答案 0 :(得分:0)
问题必定发生在遗留代码中的那一行:managed = False
。关于如何解决问题,有一些选择:
简单(干净)的方式:
由于您可以重建数据库并且没有任何您不能没有的东西,因此请擦除所有内容并进行操作。
__init__.py
以外的所有内容。./manage.py makemigrations
。./manage.py migrate
。嗯,你会失去一切,但这是“干净的石板”方法。
(可能)更好的方式:
打开您的上一次迁移(使用makemigrations
命令创建的迁移)并手动编写其余部分:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import django.utils.timezone
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('your_app', 'your_previous_migration'),
]
operations = [
# That takes care of you Meta changes
migrations.AlterModelOptions(
name='route',
options={
'managed': True,
'table': 'network_route',
},
),
# That will add the created_at field
migrations.AddField(
model_name='route',
name='created_at',
field=models.DateTimeField(
default=django.utils.timezone.now,
editable=False
),
),
# That will add the updated_at field
migrations.AddField(
model_name='route',
name='updated_at',
field=models.DateTimeField(blank=True, null=True),
]
现在你可以尝试./manage.py migrate
进行迁移,这应该可以解决问题!