【问题标题】:Store results of django databse textfield in a string list将 django 数据库文本字段的结果存储在字符串列表中
【发布时间】:2018-01-05 08:59:39
【问题描述】:

我有一个包含大量文本的文本字段的 django 模型。我希望能够查询该文本字段的数据库并将结果存储在字符串列表中。我该怎么做?

类似...

views.py:

queryset = Words.object.filter(id='1') 
wordArray = []
wordArray = queryset.split() # obviously this doesn't work

models.py

class Words (models.Model):
    dictionary = models.TextField()
    def __str__(self):
         return str(self.dictionary)

【问题讨论】:

  • 我需要它作为拆分列表

标签: python django


【解决方案1】:

试试values_list()

result = Words.object.filter(id='1').values_list('dictionary',flat=True)

然后访问每个值并对其进行拆分,如下所示:

r_list=[]
for r in result:
    r_list.append(r.split())

r_list 将在结果中包含所有值的所有单词。

【讨论】:

  • 这确实将值作为列表获取,但将每个字符存储为新条目。我需要根据空格分割它。
  • 你能发布/评论你得到的输出吗?
  • 在我看来,我正在做{% for x in object%} <li>{{X}}</li> {%endfor%},但在浏览器中它显示为 *e *x *a *m *p *l *e
  • 对象的价值是什么?是'example' 还是['example'] 你应该买后者。如果你能发布你的模型会更有帮助
  • 对不起,我不明白你在问什么。对象似乎仍然只是一长串
猜你喜欢
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多