Python的DRF字段名“”对于模型无效

2024-10-01 15:40:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的模型,我要求用户的输入

from django.db import models


# Create your models here.

class PostGDT1AndUAV(models.Model):


    latitude_gdt = models.FloatField(name='Latitude Of GDT 1',
                                     unique=True, max_length=255, blank=False,
                                     help_text="Enter the location's Latitude, first when extracting from Google Maps.",
                                     default=1)
    longitude_gdt = models.FloatField(name='Longitude Of GDT 1',
                                      unique=True, max_length=255, blank=False,
                                      help_text="Enter the location's Longitude, second when extracting from Google "
                                                "Maps.",
                                      default=1)

    latitude_uav = models.FloatField(name='Latitude Of UAV',
                                     unique=True, max_length=255, blank=False,
                                     help_text="Enter the location's Longitude, second when extracting from Google "
                                               "Maps.",
                                     default=1)
    longitude_uav = models.FloatField(name='Longitude Of UAV',
                                      unique=True, max_length=255, blank=False,
                                      help_text="Enter the location's Longitude, second when extracting from Google "
                                                "Maps.",
                                      default=1)

它是序列化程序:


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')

在django shell中打印对象实例时,出现以下错误:


django.core.exceptions.ImproperlyConfigured: Field name `latitude_gdt` is not valid for model `PostGDT1AndUAV`.

另外,我试图缩小模型中的字段,也就是说,使用一个变量

如果我使用常规python输入,我会这样做:


        gdt1_coord = input("Enter the first GDT's Lat/Lon coordinates")

        lat1, lon1 = gdt1_coord.split(',')

        lat1 = float(lat1)
        lon1 = float(lon1)

        gdt1 = [lat1, lon1]

Tags: ofthenamefromtruemodelslengthmax

热门问题