Python DRF, DETAIL: ключ ("") = (1) дублирован Ошибка - PullRequest
0 голосов
/ 12 февраля 2020

Я пытаюсь запустить этот код в django оболочке:

>>> from TwoLocationsTrian.models import PostGDT1AndUAV
>>> from TwoLocationsTrian.serializers import PostGDT1AndUAVSerializer
>>> serializer = PostGDT1AndUAVSerializer()
>>> print(repr(serializer))

, которая выдала мне эту ошибку:


Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/yovel/PycharmProjects/trialdjango/triangulation/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 537, in __repr__
    return representation.serializer_repr(self, indent=1)
  File "/home/yovel/PycharmProjects/trialdjango/triangulation/env/lib/python3.6/site-packages/rest_framework/utils/representation.py", line 77, in serializer_repr
    fields = serializer.fields
  File "/home/yovel/PycharmProjects/trialdjango/triangulation/env/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/yovel/PycharmProjects/trialdjango/triangulation/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 363, in fields
    for key, value in self.get_fields().items():
  File "/home/yovel/PycharmProjects/trialdjango/triangulation/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1063, in get_fields
    source, info, model, depth
  File "/home/yovel/PycharmProjects/trialdjango/triangulation/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1208, in build_field
    return self.build_unknown_field(field_name, model_class)
  File "/home/yovel/PycharmProjects/trialdjango/triangulation/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 1325, in build_unknown_field
    (field_name, model_class.__name__)
django.core.exceptions.ImproperlyConfigured: Field name `latitude_gdt` is not valid for model `PostGDT1AndUAV`.

Итак, я попытался изменить поля в модель:

# The updated model.

from django.db import models


class PostGDT1AndUAV(models.Model):
    """
    POST user's GDT1 and UAV locations.
    """

    latitude_gdt = models.FloatField(verbose_name='LatitudeGDT1',
                                     unique=False, max_length=255, blank=False,
                                     help_text="Enter the location's Latitude, first when extracting from Google Maps.",
                                     default=0)
    longitude_gdt = models.FloatField(verbose_name='LongitudeOfGDT1',
                                      unique=False, max_length=255, blank=False,
                                      help_text="Enter the location's Longitude, second when extracting from Google "
                                                "Maps.",
                                      default=0)

    latitude_uav = models.FloatField(verbose_name='LatitudeOfUAV',
                                     unique=False, max_length=255, blank=False,
                                     help_text="Enter the location's Longitude, second when extracting from Google "
                                               "Maps.",
                                     default=0)
    longitude_uav = models.FloatField(verbose_name='LongitudeOfUAV',
                                      unique=False, max_length=255, blank=False,
                                      help_text="Enter the location's Longitude, second when extracting from Google "
                                                "Maps.",
                                      default=0)

и сериализатор:


from rest_framework import serializers
from .models import PostGDT1AndUAV


class PostGDT1AndUAVSerializer(serializers.ModelSerializer):
    class Meta:
        model = PostGDT1AndUAV
        fields = ('latitude_gdt', 'longitude_gdt', 'latitude_uav', 'longitude_uav')

Когда я пытаюсь выполнить миграцию и выполнить миграцию после изменений (измененных с name на verbose_name и уникальных для False).

Я получаю эту ошибку:

Operations to perform:
  Apply all migrations: KnownLocations, TwoLocationsTrian, admin, auth, contenttypes, sessions
Running migrations:
  Applying TwoLocationsTrian.0003_auto_20200212_2012...Traceback (most recent call last):
  File "/home/yovel/PycharmProjects/trialdjango/triangulation/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: could not create unique index "TwoLocationsTrian_postgdt1anduav_latitude_gdt_key"
DETAIL:  Key (latitude_gdt)=(1) is duplicated.
...