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 8405259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:48:32+00:00 2026-06-09T22:48:32+00:00

Assume: class ItemToBill(models.Model): date_to_bill = fields.DateField() description = fields.charfield() customerToBill = fields.ForeignKey(Customer) I want

  • 0

Assume:

class ItemToBill(models.Model):
    date_to_bill = fields.DateField()
    description = fields.charfield()
    customerToBill = fields.ForeignKey(Customer)

I want to find all items that should be billed before today, and then group them by customer so I can create a single invoice for each customer that needs it.

for unique_customer in all_unique_customers_with_items_to_bill:
    createInvoice(unique_customer,  their_items_to_bill)

I could probably do something where I query the items (ordering by the customers) and then identifying when I’ve entered a new customer’s set of items. This would look like:

items = ItemToBill.objects.filter(date_to_bill=BEFORE_TODAY).order_by(customer)
prevCustomer = items[0].customer
customer_items = []
for item in items:
    if prevCustomer != item.customer:
        createInvoice(prevCustomer, customer_items)
        customer_items = []
        prevCustomer = item.customer
    customer_items.append(item)
createInvioce(prevCustomer, customer_items) #Handle the last customer

but there’s got to be a more clever solution. Suggestions?

  • 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-06-09T22:48:33+00:00Added an answer on June 9, 2026 at 10:48 pm

    You need a list of items by customer, which sounds like a simple loop.

    items_by_customer = {}
    
    for item in ItemToBill.objects.filter(...date_query...):
        items_by_customer.setdefault(item.customerToBill, []).append(item)
    
    for customer, items in items_by_customer.items():
        print customer, items # items grouped by customer.
        # generate_invoice(customer, items)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have this model : class Task(models.Model): title = models.CharField() Now I would
Assume class B extends class A and I want to declare a variable for
Assume this model class : public class Car extends Vehicle implements Visitable { .....
Assume we have class Employee . I want to have a field which references
So i've a class mainpercentage.m, and I want one variable in that class assume
Let's assume a User class with theese fields : @Entity public class User extends
Assume class A with methods M1 and M2 has low coupling with other classes
We all know that most applications out there assume class names to follow the
Assume a class Vehicle has a method called StartEngine, and in my subclass called
Let's assume this class in C#: public class LimitedList<T> : List<T> { private int

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.