Django-tables2: 열을 λ™μ μœΌλ‘œ μΆ”κ°€ν•˜λŠ” 직관적인 방법

에 λ§Œλ“  2012λ…„ 04μ›” 17일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: jieter/django-tables2

진행 쀑인 λͺ¨λ“  메타 마술둜 인해 ν˜„μž¬ λ‹€μŒμ„ μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

class StatsTable(tables.Table):
   class Meta:
        orderable = False
        attrs = {'class': 'tablesorter'}

    def __init__(self, form, request, *args, **kwargs):
        rf_values = form.get_values()
        rf_annotations = form.get_annotations()
        rf_filter = form.get_filter()
        rf_calculations = form.get_calculations()                  
        data = list(
            HourlyStat.objects.filter(**rf_filter).values(*rf_values).annotate(**rf_annotations).order_by('-clicks')
        ) 
        super(StatsTable, self).__init__(data, *args, **kwargs)      
        columns = rf_values + rf_annotations.keys() + rf_calculations
        for col in columns:
            self.base_columns[col] = tables.Column()     

μ‹€μ œλ‘œ μ΄λ ‡κ²Œ μ‚¬μš©ν•˜λŠ” 경우의 μœ μΌν•œ λ¬Έμ œλŠ” 쀑간에 μžˆμ–΄μ•Ό ν•˜λŠ” λ§ˆμ§€λ§‰ super()λ₯Ό ν˜ΈμΆœν•  수 μ—†λ‹€λŠ” μ‚¬μ‹€μž…λ‹ˆλ‹€. 메타 ν΄λž˜μŠ€κ°€ μ‹€μ œλ‘œ 자체적으둜 base_columnsλ₯Ό 찾지 μ•Šκ³  μƒμ†λœ λͺ¨λΈμ—μ„œλ§Œ μ°ΎκΈ° λ•Œλ¬ΈμΈ 것 κ°™μŠ΅λ‹ˆλ‹€. μ‹€μ œλ‘œ 이것은 django ν˜•μ‹μ˜ self.fields보닀 훨씬 지저뢄해 λ³΄μž…λ‹ˆλ‹€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

μ΄μƒν•˜λ‹€
self.base_columns에 동적 열을 μΆ”κ°€ν•˜μ—¬ μΆ”κ°€ν•©λ‹ˆλ‹€.

self.base_columns['column_name'] = tables.Column()

λ‚΄κ°€ μ„±κ°€μ‹  ν•œ κ°€μ§€λŠ” κ·Έλ ‡κ²Œν•¨μœΌλ‘œμ¨ μΈμŠ€ν„΄μŠ€ 속성이 μ•„λ‹Œ 클래슀 속성을 μˆ˜μ •ν•˜λ―€λ‘œ μš”μ²­μ— μΆ”κ°€ν•˜λŠ” λͺ¨λ“  열이 ν…Œμ΄λΈ”μ— λŒ€ν•œ λͺ¨λ“  후속 μš”μ²­μ— ν‘œμ‹œλ˜λ©° λ‚΄κ°€ 찾은 μ‹€μ œ 동적 열에 λŒ€ν•œ μ†”λ£¨μ…˜μž…λ‹ˆλ‹€. μ˜€λ‹€:

class ComponenteContableColumns(tables.Table):

    """Table that shows ComponentesContables as columns."""

    a_static_columns = tables.Column()

    def __init__(self, *args, **kwargs):
        # Create a copy of base_columns to restore at the end.
        self._bc = copy.deepcopy(self.base_columns)
        # Your dynamic columns added here:
        if today is 'monday':
            self.base_columns['monday'] = tables.Column()
        if today is 'tuesday':
            self.base_columns['tuesday'] = tables.Column()
        super().__init__(*args, **kwargs)
        # restore original base_column to avoid permanent columns.
        type(self).base_columns = self._bc

λͺ¨λ“  3 λŒ“κΈ€

μ΄μƒν•˜λ‹€
self.base_columns에 동적 열을 μΆ”κ°€ν•˜μ—¬ μΆ”κ°€ν•©λ‹ˆλ‹€.

self.base_columns['column_name'] = tables.Column()

λ‚΄κ°€ μ„±κ°€μ‹  ν•œ κ°€μ§€λŠ” κ·Έλ ‡κ²Œν•¨μœΌλ‘œμ¨ μΈμŠ€ν„΄μŠ€ 속성이 μ•„λ‹Œ 클래슀 속성을 μˆ˜μ •ν•˜λ―€λ‘œ μš”μ²­μ— μΆ”κ°€ν•˜λŠ” λͺ¨λ“  열이 ν…Œμ΄λΈ”μ— λŒ€ν•œ λͺ¨λ“  후속 μš”μ²­μ— ν‘œμ‹œλ˜λ©° λ‚΄κ°€ 찾은 μ‹€μ œ 동적 열에 λŒ€ν•œ μ†”λ£¨μ…˜μž…λ‹ˆλ‹€. μ˜€λ‹€:

class ComponenteContableColumns(tables.Table):

    """Table that shows ComponentesContables as columns."""

    a_static_columns = tables.Column()

    def __init__(self, *args, **kwargs):
        # Create a copy of base_columns to restore at the end.
        self._bc = copy.deepcopy(self.base_columns)
        # Your dynamic columns added here:
        if today is 'monday':
            self.base_columns['monday'] = tables.Column()
        if today is 'tuesday':
            self.base_columns['tuesday'] = tables.Column()
        super().__init__(*args, **kwargs)
        # restore original base_column to avoid permanent columns.
        type(self).base_columns = self._bc

@jmfederico 와 λ™μΌν•œ λ¬Έμ œκ°€ 있으며 μ œμ•ˆλœ ν•΄ν‚Ή(ν•΄κ²° 방법)을 μ œμžλ¦¬μ— λ„£μ–΄μ•Ό ν–ˆμŠ΅λ‹ˆλ‹€. ν…Œμ΄λΈ”μ— 열을 λ™μ μœΌλ‘œ μΆ”κ°€ν•˜λŠ” 방법에 λŒ€ν•œ 더 λ‚˜μ€ μ œμ•ˆμ΄ μžˆμŠ΅λ‹ˆκΉŒ? 맨 μœ„μ— 쿼리의 λ‚ μ§œκ°€ μžˆλŠ” ν…Œμ΄λΈ”μ΄ μžˆμœΌλ―€λ‘œ 필터링 방법에 따라 λ‚ μ§œκ°€ 더 λ§Žκ±°λ‚˜ 적을 수 μžˆμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” μ™œ 가끔 쿼리에 μ—†λŠ” μΆ”κ°€ 열이 있고 이것이 λ™μΌν•œ λ¬Έμ œμΈμ§€ μ•Œμ•„λ‚Έ 이유λ₯Ό 평생 λ™μ•ˆ ν•΄κ²°ν•  수 μ—†μ—ˆμŠ΅λ‹ˆλ‹€.

참고둜 μ—¬κΈ° λ‚΄ ν…Œμ΄λΈ” ν΄λž˜μŠ€κ°€ μžˆμŠ΅λ‹ˆλ‹€.

class ActivitySummaryTable(TableWithRawData):
    activity = tables.Column(verbose_name=_('Activity'), orderable=False)
    # the rest of the columns will be added based on the filter provided

    def __init__(self, extra_cols, *args, **kwargs):
        """Pass in a list of tuples of extra columns to add in the format (colunm_name, column)"""
        # Temporary hack taken from: https://github.com/bradleyayers/django-tables2/issues/70 to avoid the issue where
        # we got the same columns from the previous instance added back
        # Create a copy of base_columns to restore at the end.
        _bc = copy.deepcopy(self.base_columns)
        for col_name, col in extra_cols:
            self.base_columns[col_name] = col
        super(ActivitySummaryTable, self).__init__(*args, **kwargs)
        # restore original base_column to avoid permanent columns.
        type(self).base_columns = _bc

    class Meta:
        attrs = {'class': 'table'}
        order_by = ('activity',)

λ‚ μ§œλ₯Ό ν•„ν„°λ§ν•œ λ‹€μŒ 끝에 2개의 λˆ„λ½λœ λ‚ μ§œκ°€ μΆ”κ°€λœ 예
image

extra_columns 인수λ₯Ό μ‚¬μš©ν•˜μ—¬ 817d711μ—μ„œ μˆ˜μ •λ¨

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰