【问题标题】:django exedittabledataview search columnsdjango exedittabledataview 搜索列
【发布时间】:2014-12-02 16:27:44
【问题描述】:

我正在尝试在我的站点中为我的数据表创建一个搜索选项,

在 models.py 我有:

class Mymodel(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.TextField()
    body = models.TextField()
    creator = models.CharField(max_length=255, blank=True, db_index = True)
    creation_time = models.DateTimeField( db_index = True )
    event_id = models.CharField(unique=True, max_length=255)
    capture_rule = models.ForeignKey(CaptureRule, db_index = True)
    status = models.ForeignKey(EventStatus, null = True, blank=True, db_index = True)
    comment = models.TextField( null = True, blank = True )
    source_url = models.CharField(max_length=4096, null = True, blank = True )
    links = models.ManyToManyField( Link, related_name = 'events' )

在我看来.py:

class edittest(XEditableDatatableView):
model = Event
template_name = 'webapp/index.html'

datatable_options = {
    'columns':[( 'Time', 'creation_time', 'get_event_age' ),
                ( 'Status', 'status', make_xeditable( type='select' ), 'status__name' ),
                ( 'Creator', 'creator' ),
                ( 'Source', 'source' ),
                ( 'Title', 'title' ),
                ( 'Body', 'body', 'get_body_with_markup' ),
                'comment',
                'id',
                'source_url',
                ( 'Tag', 'capture_rule__tag__name' ),
                ( 'Matcher', 'capture_rule__matcher'),
                'linked_urls',
                'capture_rule'

    ],
    'search_fields': [ 'status__name' ],
    'hidden_columns' : ['body', 'comment', 'id', 'source_url', 'linked_urls', 'capture_rule' ],
    'ordering':['-creation_time'],
}

它工作正常,但是当我将 search_fields 参数更改为

'search_fields': [ 'title' ] or  'search_fields': [ 'comment' ]

它在整个列中搜索,就像我写的一样:'search_fields': []

【问题讨论】:

  • 旁注,django 已经添加了 id 主键,为什么还要添加 id = models.AutoField(primary_key=True) 呢?另外,在您的问题中,MyModel 是否等于 Event?
  • 你说得对,但同时我专注于搜索选项

标签: django x-editable


【解决方案1】:

这可能是因为搜索字段不应该已经出现在列列表中。

总是附加到列表中的类似过滤器的 ORM 字段列表 对表执行搜索时的搜索字段。 search_fields 应该只包含指向非字段的 ORM 路径 已经在列定义中,因为那些已经被搜索 默认。

https://pypi.python.org/pypi/django-datatable-view/0.5.5

【讨论】:

  • 知道为什么当我搜索源列中的某个表达式时找不到它吗??
  • @OmegaDoe 您能否举例说明实际查询和数据库中的数据,如果与原始问题严格相关,可能会修改您的问题。
猜你喜欢
  • 2018-03-27
  • 2022-07-22
  • 2017-12-02
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
相关资源
最近更新 更多