Я пытаюсь создать модель с геопространственным полем. Когда я пытаюсь выполнить миграцию, я получаю следующую ошибку:
Apply all migrations: admin, auth, contenttypes, map, sessions
Running migrations:
Applying map.0008_territorio_geometry...Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\Marco\Envs\um\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\core\management\commands\migrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\migrations\operations\fields.py", line 110, in database_forwards
schema_editor.add_field(
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\backends\base\schema.py", line 450, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\backends\base\schema.py", line 223, in column_sql
default_value = self.effective_default(field)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\backends\base\schema.py", line 303, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\models\fields\__init__.py", line 821, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\contrib\gis\db\models\fields.py", line 147, in get_db_prep_value
super().get_db_prep_value(value, connection, *args, **kwargs),
File "C:\Users\Marco\Envs\um\lib\site-packages\django\db\models\fields\__init__.py", line 816, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\Marco\Envs\um\lib\site-packages\django\contrib\gis\db\models\fields.py", line 190, in get_prep_value
raise ValueError('Cannot use object with type %s for a spatial lookup parameter.' % type(obj).__name__)
ValueError: Cannot use object with type int for a spatial lookup parameter.
Я также пытался удалить атрибуты из моей модели, но ничего не изменилось. Ниже приведены мои models.py
from django.contrib.gis.db import models
# Create your models here.
class Piano(models.Model):
"""Modello che rappresenta un piano"""
titolo = models.CharField(max_length=100, help_text='Nome del piano')
acronimo = models.CharField(max_length=100, help_text='Acronimo del piano')
#fk_territorio = models.ForeignKey('Territorio', verbose_name='Territorio', on_delete=models.SET_NULL, null=True)
def __str__(self):
"""String for representing the Model object."""
return self.titolo
class Meta:
verbose_name_plural = "Piani"
class Territorio(models.Model):
"""Modello che rappresenta la tipologia di una norma"""
nome = models.CharField(max_length=100, help_text='Nome del territorio')
fk_tipo_territorio = models.ForeignKey('TipoTerritorio', verbose_name='Tipo Territorio', on_delete=models.SET_NULL, null=True)
parent = models.ForeignKey('self', blank=True, related_name='child', help_text='Parent del Territorio', on_delete=models.SET_NULL, null=True)
codice = models.PositiveIntegerField(help_text='Codice del Territorio', null=True)
geometry = models.GeometryField(spatial_index=True, null=True)
def __str__(self):
"""String for representing the Model object."""
return self.nome
class Meta:
verbose_name_plural = "Territori"
class TipoTerritorio(models.Model):
"""Modello che rappresenta la tipologia di una norma"""
nome = models.CharField(max_length=100, help_text='Tipologia di territorio')
def __str__(self):
"""String for representing the Model object."""
return self.nome
class Meta:
verbose_name_plural = "TipiTerritorio"
Я относительно новичок в Django и Geo Django и могу что-то пропустить. Любая идея о том, как решить ошибку переноса?
РЕДАКТИРОВАТЬ: добавлен 0008_territorio_geometry.py
# Generated by Django 3.0.4 on 2020-04-06 13:19
import django.contrib.gis.db.models.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('map', '0007_auto_20200406_1506'),
]
operations = [
migrations.AddField(
model_name='territorio',
name='geometry',
field=django.contrib.gis.db.models.fields.GeometryField(default=0, srid=4326),
preserve_default=False,
),
]