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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:33:46+00:00 2026-06-04T16:33:46+00:00

class Beverage(models.Model): name=models.CharField(max_length=255) def __unicode__(self): return self.name class Location(models.Model): name=models.CharField(max_length=255) beverages = models.ManyToManyField(Beverage, through=’LocationStandard’)

  • 0
class Beverage(models.Model):
    name=models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

class Location(models.Model):
    name=models.CharField(max_length=255)
    beverages = models.ManyToManyField(Beverage, through='LocationStandard')
    location_number=models.CharField(max_length=255)
    organization=models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

class LocationStandard(models.Model):
    beverage=models.ForeignKey(Beverage)
    location=models.ForeignKey(Location) #elim this or m2m
    start_units=models.IntegerField()
    fill_to_standard=models.IntegerField(max_length=10)
    order_when_below=models.IntegerField(max_length=10)

class Order(models.Model):
    location=models.ForeignKey(Location) #elim this or m2m
    beverage=models.ForeignKey(Beverage)
    units_ordered=models.IntegerField(max_length=10, default=0)
    order_delivered=models.BooleanField(default=False)
    timestamp=models.DateTimeField(auto_now_add=True)
    user=models.ForeignKey(User)

How can I generate a report that will get me an HTML table with all locations on the x-axis and all beverages on the y-axis. The main thing I am struggling with is just what to query that I can pass the template that I can loop over. Thoughts?

  • 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-04T16:33:48+00:00Added an answer on June 4, 2026 at 4:33 pm

    You can’t get them in one query, but you can make something like that (don’t want to setup a whole env to test, so use it as a clue, not a working solution):

    # you can't do order_by in a template, either do them in the view, or 
    # make methods in the model, or make it the Meta default ordering
    
    # print the header, and make sure we got a list of all beverage cached
    beverages = Beverage.objects.order_by('name') 
    for beverage in beverages:
       print each cell of your header
    
    # print a row for each location
    locations = Location.objects.all()
    for location in locations:
       print the location name table cell
       location_beverages = iter(location.beverages.order_by('name'))
       # for each beverage, we print a cell. If the beverage is in the 
       # location beverage list, we print a checked cell
       # we use the fact that queryset are iterable to move the cursor
       # only when there is a match
       cur_bev = location_beverages.next()
       for beverage in beverages:
            if beverage == cur_bev:
                print checked table cell
                cur_bev = location_beverages.next()
            else:
                print empty table cell
    

    The intermediary variables to store querysets are very important, as they allow you to benefit from Django queryset cache.

    With Django 1.4 or more, you can replace:

    locations = Location.objects.all()
    

    By:

    locations = Location.objects.prefetch_related('beverages')
    

    To get a serious perf boost.

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

Sidebar

Related Questions

class Book(models.Model): name = models.CharField(max_length=127, blank=False) class Author(models.Model): name = models.CharField(max_length=127, blank=False) books =
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
class ITestType(object): Sample interface type __metaclass__ = ABCMeta @abstractmethod def requiredCall(self): return class TestType1(object):
Class Bar inherits from Foo: class Foo(object): def foo_meth_1(self): return 'foometh1' def foo_meth_2(self): return
class StartAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): print "Hello" start.add_argument('-s', '--start', action=StartAction) I
class A: pass def b(self): print('b') A.b = b a = A() At this
class Anketum < ActiveRecord::Base has_one :user class << self def search(params) self.scope :h, :conditions
class Order(models.Model): ... class OrderItem(models.Model) order = models.ForeignKey(Order) product = models.ForeignKey(Product) quantity = models.PositiveIntegerField()
class A: def x ( self ): print( self.__class__ ) class B ( A

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.