문제

I'm moving my tables to django-tables2. By now is almost everything working ok, but now I have a problem.

In my current version i use checkboxes to select items

<td><input type="checkbox" class="checkbox_delete" name="event" id="event.id"
                                               value="{{ event.id }}" />

this way in the view i can recover the event.id using request.POST.getlist('event')

Now I'm trying to add the "value" attribute to a CheckBoxColumn

select = tables.CheckBoxColumn(attrs={'td__input': {'class': 'checkbox_delete', 'name': 'event', **'value': [A('id')]**}, 'th__input': {'id': 'selectAll'}},
                                   empty_values=())

I've been playing with the Accesor and the record.id which I use in a templateColumn.

How can I pass the id to the value attribute??

도움이 되었습니까?

해결책 2

I found another solution here How to get information from Django_tables2 row?

Just need to define select = tables.CheckBoxColumn(accessor='pk') and it add the value as the record.id

다른 팁

You can simply do something like this:

id = tables.CheckBoxColumn()

This way, the column will be rendered like this

<input type="checkbox" name="id" value="X">

where X will be the value of the id field.

Now for a more complete answer:

You can add td__input to override some defaults but I don't believe what you can set it to different values for each column ! By checking the source:

https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/columns/checkboxcolumn.py

you will see that in the render method in CheckBoxColumn will create an AttributeDict containing the attributes from input, td__input and some defaults, like this:

def render(self, value, bound_column):  # pylint: disable=W0221
        default = {
            'type': 'checkbox',
            'name': bound_column.name,
            'value': value
        }
        general = self.attrs.get('input')
        specific = self.attrs.get('td__input')
        attrs = AttributeDict(default, **(specific or general or {}))
        return mark_safe('' % attrs.as_html())

So the attrs you define will be the same in all columns since attrs.as_html will just convert the 'x':'y' dict entries to x=y...

So if you want to have total control and do whatever you like with the values for each column, just subclss CheckBoxColumn and override render (left as an excersise to the reader).

Update

Also, a very nice thing about your own render method is that you don't need to define the same parameters as the base one. This is because django-tables2 uses the getargspec function to find out which arguments your render expects and pass them to the render method. So, if you take a look at the https://github.com/bradleyayers/django-tables2/blob/master/django_tables2/rows.py you will see that the available parameters that can be passed to render, along with their values are:

available = {
      'value':        value,
      'record':       self.record,
      'column':       bound_column.column,
      'bound_column': bound_column,
      'bound_row':    self,
      'table':        self._table,
}

So, for instance you can define your render method like: def render(self, value, bound_column, record):

to also pass the record to it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top