Django 更改下拉菜单
Django changes dropdown menu
下面开始代码,专门针对models和views -
class userchoice(models.Model):
choice = []
choice2 = list(userdata.objects.all().values_list('key'))
for x in choice2:
for t in x:
choice.append((t,t.capitalize()))
Job_ID = models.CharField(primary_key = True, max_length=200, choices=choice, default='key')
Group_ID= models.CharField(max_length=200, choices=choice, default='key')
Customer_name = models.CharField(max_length=200, choices=choice, default='key')
Planned_Duration = models.CharField(max_length=200, choices=choice, default='key')
Worker_name = models.CharField(max_length=200, choices=choice, default='key')
Job_note = models.CharField(max_length=200, choices=choice, default='key')
Customer_tp_name = models.CharField(max_length=200, choices=choice, default='key')
从 django.db 导入迁移、模型
class 迁移(migrations.Migration):
dependencies = [
('csvfileapp', '0008_userdata'),
]
operations = [
migrations.CreateModel(
name='userchoice',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('Job_ID', models.CharField(default='key', max_length=200)),
('Group_ID', models.CharField(default='key', max_length=200)),
('Customer_name', models.CharField(default='key', max_length=200)),
('Planned_Duration', models.CharField(default='key', max_length=200)),
('Worker_name', models.CharField(default='key', max_length=200)),
('Job_note', models.CharField(default='key', max_length=200)),
('Customer_tp_name', models.CharField(default='key', max_length=200)),
],
),
]
到目前为止,这根本不是一个好的解决方案。因为选择可以随时更改(因为它正在获取数据库结果)所以任何更改都会导致新的迁移文件不好。按如下操作:
- 去掉那个部分,让它变得简单
charField
- 在您的 forms.py 中启动您想要选择的字段与您想要的数据库查询结果。
总而言之,在视图和表单中处理这种情况,而不是数据库 orm 和迁移!
更新:
这是要使用的 forms.py 的代码示例:
class waypointForm(forms.Form):
def __init__(self, user, *args, **kwargs):
super(waypointForm, self).__init__(*args, **kwargs)
self.fields['waypoints'] = forms.ChoiceField(
choices=[(o.id, str(o)) for o in Waypoint.objects.filter(user=user)]
这是stack link for it
下面开始代码,专门针对models和views -
class userchoice(models.Model):
choice = []
choice2 = list(userdata.objects.all().values_list('key'))
for x in choice2:
for t in x:
choice.append((t,t.capitalize()))
Job_ID = models.CharField(primary_key = True, max_length=200, choices=choice, default='key')
Group_ID= models.CharField(max_length=200, choices=choice, default='key')
Customer_name = models.CharField(max_length=200, choices=choice, default='key')
Planned_Duration = models.CharField(max_length=200, choices=choice, default='key')
Worker_name = models.CharField(max_length=200, choices=choice, default='key')
Job_note = models.CharField(max_length=200, choices=choice, default='key')
Customer_tp_name = models.CharField(max_length=200, choices=choice, default='key')
从 django.db 导入迁移、模型
class 迁移(migrations.Migration):
dependencies = [
('csvfileapp', '0008_userdata'),
]
operations = [
migrations.CreateModel(
name='userchoice',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('Job_ID', models.CharField(default='key', max_length=200)),
('Group_ID', models.CharField(default='key', max_length=200)),
('Customer_name', models.CharField(default='key', max_length=200)),
('Planned_Duration', models.CharField(default='key', max_length=200)),
('Worker_name', models.CharField(default='key', max_length=200)),
('Job_note', models.CharField(default='key', max_length=200)),
('Customer_tp_name', models.CharField(default='key', max_length=200)),
],
),
]
到目前为止,这根本不是一个好的解决方案。因为选择可以随时更改(因为它正在获取数据库结果)所以任何更改都会导致新的迁移文件不好。按如下操作:
- 去掉那个部分,让它变得简单
charField
- 在您的 forms.py 中启动您想要选择的字段与您想要的数据库查询结果。
总而言之,在视图和表单中处理这种情况,而不是数据库 orm 和迁移!
更新:
这是要使用的 forms.py 的代码示例:
class waypointForm(forms.Form):
def __init__(self, user, *args, **kwargs):
super(waypointForm, self).__init__(*args, **kwargs)
self.fields['waypoints'] = forms.ChoiceField(
choices=[(o.id, str(o)) for o in Waypoint.objects.filter(user=user)]
这是stack link for it