Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7519371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:53:29+00:00 2026-05-30T01:53:29+00:00

i was looking through the django documentation on how to export my data to

  • 0

i was looking through the django documentation on how to export my data to a CSV file so it can be imported into excel. So far i got it working but im having a bit of trouble figuring out how i can export the ManyToMany Fields in my models.

So far i have

writer = csv.writer(response)
writer.writerow(['A','B', 'C', 'D'])

    for x in case_log_list:
        writer.writerow([x.A,
                x.B,
                for y in x.C.all:
                    y,
                x.D)

where the for loop inside the [list] is where i was trying to iterate over the ManyToMany Objects in that field. However i keep getting a syntax error that doesn’t make any sense.. am i even going about this correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:53:30+00:00Added an answer on May 30, 2026 at 1:53 am
    1. You’re missing a terminating ] on your list definition.
    2. You can’t put a for loop inside a list.

    Is x.C a list or sequence of some kind? Is your intent to write each item of x.C as a separate field in the CSV?

    I believe you intended to use the list concatenation operator +, which takes two lists and glues them together into one list. Use it like so:

    >>> a = [1, 2, 3]
    >>> b = [4, 5, 6]
    >>> a + b
    [1, 2, 3, 4, 5, 6]
    

    Your example becomes:

    for x in case_log_list:
        c_list = [item for item in x.C.all]          # turn x.C into a list
        output_list = [x.A, x.B] + c_list + [x.D,]   # assemble list
        writer.writerow( output_list )
    

    Or if you want to be brief:

    for x in case_log_list:
        writer.writerow ( [x.A, x.B] + [item for item in x.C.all] + [x.D,] )
    

    Notes:

    • If x.C.all is already a list – that is, if type(x.C.all) is list is True – then [x.A, x.B] + x.C.all + [x.D,] will suffice.

    • c_list = [item for item in x.C.all] is a list comprehension, which lets you do things to a list without having to write a for loop.

      List comprehensions are very powerful, very elegant and you should use them whereever it makes sense to do so. Any time you need to make a list based on another list, filter items in a list by some criteria, or both at the same time, a list comprehension is probably a good way to go.

    • Single-letter variable names like x, A, B, C, D are really terrible because they give no information about what data the variable holds, what its type may be, what values it might contain, etc. Use more descriptive variable names if you can.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just looking through some information about Google's protocol buffers data interchange format.
I was looking through the Java documentation, and I came across the clear() method
I'm sending queries through Django on a PaaS service, and I think I can't
Looking through the Django docs and trying to figure out the use of the
So I'm looking at the user authentication for django, and since it's turning into
Looking through our MMC snap-in code I can't get one concept. We have a
I want to dig deeper into Django's internals and the official online documentation only
I was looking over the Django documentation on a way to do this but
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
Looking through my server logs, I see that a lot of pages on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.