自动填充字段从Django的数据库

问题描述 投票:1回答:1

models.朋友

class InfraServiceInfo(db.Model):

    app_name = db.CharField(choices=VIEW_APP_CHOICES, max_length=1000)
    stack = db.CharField(max_length=1000)
    description = db.TextField('InfraServiceInfo', choices=VIEW_DESC_CHOICES)

需要说明的是,在该InfraServiceInfo表中的每个APP_NAME相关。作为APP_NAME是下拉列表中,当选择一个特定APP_NAME与APP_NAME相关联的对应的描述应当被自动在形式填充。

例如:APP_NAME = “测试”,描述= “测试” 和APP_NAME = “test2的”,描述= “testing2”。当从下降downlist用户选择测试,描述应自动填充测试字符串。我该怎么做呢?

django python-3.x django-models django-forms django-views
1个回答
0
投票

我认为你需要奉献不同url以满足您的选择InfraServiceInfo,在那里你可以显示所选的描述。

说你的网址是像

url
url(r'^infraServiceInfo/$', 'myapp.views.infraServiceInfo', name='InfraServiceInfo'),
url(r'^infraServiceInfo/(?P<slug>[\w-]+)/$', 'myapp.views.infraServiceInfo_detail', name='InfraServiceInfo_detail'),

而在你infraServiceInfo_detail

def infraServiceInfo_detail(request, app_name):

    description = InfraServiceInfo.objects.get(app_name=app_name).description
    app_names = InfraServiceInfo.objects.values_list('app_name', flat=True)
    return render(request, 'your_render.html', {'description': description, 'app_names': app_names})
© www.soinside.com 2019 - 2024. All rights reserved.