python - Python,Django rest框架,创建一个包含选项或自由文本的字段
问题描述
我有一个 Django 休息框架 API。
我希望向其中添加一个字段,其中包含选项或自由文本。
我希望如果用户决定向该字段添加一个新值,它将被添加到数据库中。
这
我有以下现有模型:
class Points(models.Model):
mission_name = models.CharField(name='MissionName',
verbose_name="Mission Name",
unique=True,
max_length=255,
blank=False,
help_text="Enter the mission's name"
)
location_name = models.CharField(choices= # fetch known locations from the DB
#or create a new one)
# Show existing options or input a new one.
# If an existing location has been chosen then it's Latitude and Longitude are of the fetched object.
# Pretty sure that this kind of action belongs in the serializer or the view.
latitude = models.FloatField(name="GDT1Latitude",
verbose_name="GDT 1 Latitude",
unique=False, max_length=255, blank=False,
help_text="Enter the location's Latitude, first when extracting from Google Maps.",
default=DEFAULT_VALUE)
longitude = models.FloatField(name="GDT1Longitude",
verbose_name="GDT 1 Longitude",
unique=False, max_length=255, blank=False,
help_text="Enter the location's Longitude, second when extracting from Google Maps.",
default=DEFAULT_VALUE)
area = models.CharField(
name='Area',
max_length=8,
choices=AREAS,
)
解决方案
您应该制作另一个模型位置,并将外键放入您的点模型中:
enter code here
class Points(models.Model):
location_name = models.ForeignKey(Location,on_delete=models.SET_NULL,null=true)
class Location(models.Model):
#any info you want to store
推荐阅读
- flutter - 如何根据检测到颤振应用程序中的锁定或关闭屏幕采取行动
- ios - SwiftUI IPhone TextFields spanning greater than screen jumping, standard behaviour?
- c++ - MFC C++ - MDI 选项卡式视图下拉列表菜单消失
- php - 如何从 laravel 的数据库中检索内爆图像并在幻灯片中显示
- ios - SwiftUI - 如何每天洗牌一次?
- android - 当我将 MediaQuery.of(context).padding.top 与 appBar 一起使用时,它返回 0.0。这是为什么?
- reactjs - “this.props”在哪里定义?
- facebook - 创建 Facebook 页面 - facebook api
- python - 在python中加入数据框列
- reactjs - 下一个图像不采用类属性