python - Python 的 DRF,字段名称“”对模型无效
问题描述
我有以下模型,我要求用户输入。
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]
解决方案
kwargname
是数据库中字段的名称,因此不能包含任何空格。尝试将其替换为verbose_name
推荐阅读
- python - 具有 UINT8 权重的 TensorFlow 2.0 TFLite 量化导出
- charts - SwiftUI 中的图表作为气泡图
- flutter - 如何使文本适合带有图标的圆形按钮
- python - 当我使用某个函数时,如何查看正在执行的代码?
- python - 当出现 TypeError: +: 'type' and 'type' 不支持的操作数类型时,我该如何解决?
- node.js - 错误:queryTxt ETIMEOUT cluster0.hbnks.mongodb.net
- python - Discord py,bot 两次发送消息
- java - GRPC 生成源 - Java IntelliJ
- android - 尝试创建构建目标时,PixelExperience build.ninja 失败
- reactjs - 触摸字段会导致 react-hook-form 变脏