如何从模型中获取现场数据?

发布于 2024-10-31 01:38:45 字数 1275 浏览 2 评论 0原文

假设我有一个名为 app 的 Django 应用程序。在其中,我在 models.py 中定义了一个名为 data 的模型类。另外,我的模板目录中有一个 html 文件,其中有几个选项列表,其中应该填充模型的字段,例如类型、日期。 我需要做的是获取views.py 中的字段列表并用它们填充选项列表。另一件事是,在用户通过选择列表中的值提交查询后,我将向他们展示一个显示模型中所有字段的值的表格。所以基本上我必须检索用户输入的内容,并通过输入进行查询,并根据查询获取模型中所有字段的数据。我们可以命名另外两个字段 time、value。这些事情大部分应该在views.py中完成。

以下是 hmtl 文件中的选项列表:

 <form action="" method="post">
 <select name="type" values="type_data"></select>
 <select name="date" values="date_data"></select>
 <input type="submit" value="Submit">
 </form>

我尝试过的两件事是:

1.为了获取数据来填充选项列表,我使用了:

objects= data.objects.all()
type_data=getattr(objects, 'type')
date_data=getattr(objects, 'date')

2.为了在查询后获取数据,我使用了:

if request.method == 'POST':
     …
    mytype =  '% r' % request.POST.get['type']
   mydate = ' %r ' % request.POST.get['date']
   objects2= data.objects2.filter(type=mytype,date=mydate)
   time=getattr(objects2, 'time')
   value=getattr(objects2, 'value')

它不工作。我引用此链接 django object get/set field 所以我基本上使用这个从模型获取字段数据:getattr(obj, 'field_name')

有什么想法吗?我很感激任何帮助

Say I have a Django application named app. Within it I have a model class defined in models.py named data. Also, I have a html file in the template directory that has several picklists, which should be populated with the fields of the model, such as type, date.
What I need to do is to get the lists of the fields in the views.py and populate the picklists with them. Another thing is that, after the user submit their query by choosing a value in the picklist, I will show them a table displaying the values of all the fields in the model. So basically I have to retrieve what the user inputs, and query by the input, and get the data of all the fields in the model based on the query. We could name another two fields time, value. These things should be mostly done in views.py.

Here are the picklists in the hmtl file:

 <form action="" method="post">
 <select name="type" values="type_data"></select>
 <select name="date" values="date_data"></select>
 <input type="submit" value="Submit">
 </form>

What I tried, for the two things, are:

1.To get the data to populate the picklist, I used:

objects= data.objects.all()
type_data=getattr(objects, 'type')
date_data=getattr(objects, 'date')

2.To get the data after the query, I used:

if request.method == 'POST':
     …
    mytype =  '% r' % request.POST.get['type']
   mydate = ' %r ' % request.POST.get['date']
   objects2= data.objects2.filter(type=mytype,date=mydate)
   time=getattr(objects2, 'time')
   value=getattr(objects2, 'value')

It does not work. I am referencing this link django object get/set field so I’m basically using this to get field data from model: getattr(obj, 'field_name')

Any thoughts? I appreciate any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-11-07 01:38:45

看看 Django 的 Form 功能 - 特别是 ModelChoiceField – 它解决了这个问题。

如果您想内省模型以查看它具有哪些字段,请查看 model._meta.fields

例如,以下代码打印模型实例 instance 上每个字段的值:

for field in instance._meta.fields:
    print field.attname, '=', getattr(model, field.attname)

Have a look at Django's Form functionality – specifically ModelChoiceField – it solves this problem.

If you want to introspect a model to see what fields it has, have a look at model._meta.fields.

For example, the following code prints out the value for each field on a model instance instance:

for field in instance._meta.fields:
    print field.attname, '=', getattr(model, field.attname)
简单气质女生网名 2024-11-07 01:38:45

您的 objects 变量是一个查询集,当具体化时是一个“对象列表”,因此要获取对象属性列表,您应该迭代它们:

objects = data.objects.all()

type_data = []
date_date = []

for obj in objects:
    type_data.append(obj.type)
    date_date.append(obj.date)

但我认为下拉列表真正需要的是每个值都有一个 id(但我可能是错的):

for obj in objects:
    type_data.append((obj.pk, obj.type))
    date_date.append((obj.pk, obj.date))

这将创建一个元组列表:

[(1, 'value1'), (2, 'value2')...]

因此在模板中您可以使用:

<select name="type">
    {% for value, name in type_data %}
        <option value="{{ value }}">{{ name }}</option>
    {% endfor %}
</select>

但请记住,django 可以简化此过程 - 阅读有关表单的内容。

your objects variable is a QuerySet, when materialized is a "list of objects", so to get list of object properties you should iterate over them:

objects = data.objects.all()

type_data = []
date_date = []

for obj in objects:
    type_data.append(obj.type)
    date_date.append(obj.date)

but i suppose what you will really need for the dropdown is to have an id for each value (but i may be wrong):

for obj in objects:
    type_data.append((obj.pk, obj.type))
    date_date.append((obj.pk, obj.date))

this will create a list of tuples:

[(1, 'value1'), (2, 'value2')...]

so in the template you can use:

<select name="type">
    {% for value, name in type_data %}
        <option value="{{ value }}">{{ name }}</option>
    {% endfor %}
</select>

but have in mind, that django can simplify this process - read about forms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文