【问题标题】:Auto populate fields from database in django在 django 中自动填充数据库中的字段
【发布时间】:2019-02-04 16:59:03
【问题描述】:

models.py

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 相关联。 As app_name is a drop down list, when the selects a particular app_name the corresponding description associated with that app_name should be auto populated in the forms.

示例:app_name = "test",description = "testing" 和 app_name = “test2”,描述 = “testing2”。 当用户从下拉列表中选择测试时,描述应自动填充测试字符串。我该怎么做?

【问题讨论】:

    标签: django python-3.x django-models django-forms django-views


    【解决方案1】:

    我认为您需要使用不同的url 视图来为您选择的InfraServiceInfo 提供服务,您可以在其中显示选择的描述。

    说你的网址是这样的

    网址
    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})
    

    【讨论】:

      猜你喜欢
      • 2010-09-29
      • 1970-01-01
      • 2016-12-22
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 2021-07-17
      相关资源
      最近更新 更多