Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
print field names of table in model to xls sheet
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
danin  
View profile  
 More options Oct 10, 7:31 pm
From: danin <gawade.ni...@gmail.com>
Date: Sat, 10 Oct 2009 02:31:48 -0700 (PDT)
Local: Sat, Oct 10 2009 7:31 pm
Subject: print field names of table in model to xls sheet
Hi guys,
 can anyone please tell how can i print field name of model to xls
sheet. for eg

general_address=models.CharField(max_length=100)

i want to print the "general address" as my column heading in my xls
sheet. i dnt want to hardcode it in the sheet.write.

Thanks n regards


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Oct 10, 8:07 pm
From: John Machin <sjmac...@lexicon.net>
Date: Sat, 10 Oct 2009 21:07:30 +1100
Local: Sat, Oct 10 2009 8:07 pm
Subject: Re: [pyxl] print field names of table in model to xls sheet
On 10/10/2009 8:31 PM, danin wrote:

> Hi guys,
>  can anyone please tell how can i print field name of model to xls
> sheet. for eg

> general_address=models.CharField(max_length=100)

I presume that you mean that the above statement causes general_address
to refer to a string (str or unicode object).

Just so that we're absolutely sure, please execute the following statement:
     print type(general_address), repr(general_address)
and reply back with a copy/paste of the output.

> i want to print the "general address" as my column heading in my xls
> sheet. i dnt want to hardcode it in the sheet.write.

Of course you don't want to hardcode it .... what is wrong with doing this:
     # Assuming for purposes of the example that you want this
     # to appear in cell C1
     worksheet.write(0, 2, general_address)
?

Cheers,
John


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Withers  
View profile  
 More options Oct 10, 8:13 pm
From: Chris Withers <ch...@simplistix.co.uk>
Date: Sat, 10 Oct 2009 11:13:13 +0100
Local: Sat, Oct 10 2009 8:13 pm
Subject: Re: [pyxl] print field names of table in model to xls sheet

danin wrote:
> general_address=models.CharField(max_length=100)

> i want to print the "general address" as my column heading in my xls
> sheet. i dnt want to hardcode it in the sheet.write.

This is really a Django question, but since I've recently done something
similar myself...

If you're starting off with an object, you'd do:

model = object.__class__

If you already have a model, you'd then do:

names = []
headings = []
for field in model._meta.fields:
   if name=='id':
     # you may want to skip auto-generated primary keys
     continue
   if getattr(f,'related',None) is not None:
     # you may want to skip foreign keys
     continue
    names.append(field.name)
   headings.append(field.verbose_name)

The to dump, for example, all the instances of this model to a file,
you'd do:

from xlwt import Workbook
book = Workbook()
sheet = book.add_sheet(YourModel.__name__+' instances')

for col,heading in enumerate(headings):
   sheet.write(0,col,heading)

for row,obj in enumerate(YouModel.objects.all()):
   for col,name in enumerate(names):
     sheet.write(row+1,col,getattr(obj,name))

book.save(YourModel.__name__+'.xls')

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
danin  
View profile  
 More options Oct 10, 8:22 pm
From: danin <gawade.ni...@gmail.com>
Date: Sat, 10 Oct 2009 03:22:32 -0700 (PDT)
Local: Sat, Oct 10 2009 8:22 pm
Subject: Re: print field names of table in model to xls sheet
address_general = models.CharField(_('Address'), max_length = 100,
blank = True,null=True)
This is my exact line from model

On Oct 10, 3:07 pm, John Machin <sjmac...@lexicon.net> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Withers  
View profile  
 More options Oct 10, 8:26 pm
From: Chris Withers <ch...@simplistix.co.uk>
Date: Sat, 10 Oct 2009 11:26:59 +0100
Local: Sat, Oct 10 2009 8:26 pm
Subject: Re: [pyxl] Re: print field names of table in model to xls sheet

danin wrote:
> address_general = models.CharField(_('Address'), max_length = 100,
> blank = True,null=True)
> This is my exact line from model

Danin,

Please be aware that it takes quite a psychic leap to guess that you are
talking about a Django model here.

This is *not* the Django list and most of the people on this list do not
know or care what Django is ;-) I've tried to provide help as best I can
in my other reply, but if you have Django questions you're likely to get
better help on the Django list:

http://groups.google.com/group/django-users

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
danin  
View profile  
 More options Oct 10, 8:49 pm
From: danin <gawade.ni...@gmail.com>
Date: Sat, 10 Oct 2009 03:49:41 -0700 (PDT)
Local: Sat, Oct 10 2009 8:49 pm
Subject: Re: print field names of table in model to xls sheet
ya i got it. thanks. :)

On Oct 10, 3:26 pm, Chris Withers <ch...@simplistix.co.uk> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Withers  
View profile  
 More options Oct 10, 8:51 pm
From: Chris Withers <ch...@simplistix.co.uk>
Date: Sat, 10 Oct 2009 11:51:40 +0100
Local: Sat, Oct 10 2009 8:51 pm
Subject: Re: [pyxl] Re: print field names of table in model to xls sheet

danin wrote:
> ya i got it. thanks. :)

Care to elaborate?

What did you "get"? What code are you using? Has it worked out for you?

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Yeung  
View profile  
 More options Oct 11, 2:28 am
From: John Yeung <gallium.arsen...@gmail.com>
Date: Sat, 10 Oct 2009 12:28:24 -0400
Local: Sun, Oct 11 2009 2:28 am
Subject: Re: [pyxl] Re: print field names of table in model to xls sheet

On Sat, Oct 10, 2009 at 6:51 AM, Chris Withers <ch...@simplistix.co.uk> wrote:

> danin wrote:
>> ya i got it. thanks. :)

> What did you "get"?

> Chris

Maybe he got the message that the Django group is a better place for
his inquiries than the Python Excel group?  (We can hope, right? :)

Or, perhaps your earlier code examples were enough to get him where he
needed to go.

John Y.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Withers  
View profile  
 More options Oct 21, 12:31 am
From: Chris Withers <ch...@simplistix.co.uk>
Date: Tue, 20 Oct 2009 15:31:52 +0100
Local: Wed, Oct 21 2009 12:31 am
Subject: Re: [pyxl] Re: print field names of table in model to xls sheet

John Yeung wrote:
> Maybe he got the message that the Django group is a better place for
> his inquiries than the Python Excel group?  (We can hope, right? :)

> Or, perhaps your earlier code examples were enough to get him where he
> needed to go.

Yeah, I just get frustrated with people rude enough to post their half
formed and semi incomprehensible questions and, when the mind reading
does actually work, who then bugger off without ever even explaining
what happened...

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google