2013-03-07 90 views
1
def export_as_xls(modeladmin, request, queryset): 
    """ 
    Generic xls export admin action. 
    """ 
    if not request.user.is_staff: 
     raise PermissionDenied 
    opts = modeladmin.model._meta 

    wb = Workbook() 
    ws0 = wb.add_sheet('0') 
    col = 0 
    field_names = [] 
    # write header row 
    for field in opts.fields: 
     ws0.write(0, col, field._meta.verbose_name) 
     field_names.append(field._meta.verbose_name) 
     col = col + 1 

如果我嘗試出口XLS我有這樣的錯誤:下拉列表AutoField對象有沒有屬性「_meta - field._meta.verbose_name

'AutoField' object has no attribute '_meta'

有了:field.name這個工作,但我需要冗長的名字

回答

0

對於字段,您應該只使用field.verbose_name

_meta可用於模型實例,但不適用字段實例

相關問題