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

  • Home
  • SEARCH
  • 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 851549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:26:43+00:00 2026-05-15T07:26:43+00:00

The model: class Product(models.Model): name = models.CharField(max_length = 128) def __unicode__(self): return self.name class

  • 0

The model:

class Product(models.Model):
  name = models.CharField(max_length = 128)
  def __unicode__(self):
    return self.name

class Receipt(models.Model):
  name = models.CharField(max_length=128)
  components = models.ManyToManyField(Product, through='ReceiptComponent')
  class Admin:
    pass

  def __unicode__(self):
    return self.name

class ReceiptComponent(models.Model):
  product = models.ForeignKey(Product)
  receipt = models.ForeignKey(Receipt)
  quantity = models.FloatField(max_length=9)
  unit = models.ForeignKey(Unit)
  def __unicode__(self):
    return unicode(self.quantity!=0 and self.quantity or '') + ' ' + unicode(self.unit) + ' ' + self.product.genitive

The idea:
there are a components on stock. I’d like to find out which recipes I can made with components which I have.

It’s not easy – but possible – I made a SQL view, which gets the solution. But I’m learning python and Django so I’d like to make it Django-style ;D

The concept of solution:

  1. get the set of recipes which has at last one component:

    list_of_available_components = ReceiptComponent.objects.filter(product__in=list_of_available_products).distinct()
    list_of_related_receipts = Receipt.objects.filter(receiptcomponent__in = list_of_available_components).distinct()

  2. get recipes (from list_of_related_receipts) which has not at last one component

    list_of_incomplete_recipes = (SELECT * FROM drinkbook_receiptcomponent LEFT JOIN drinkstore_stock_products USING(product_id) WHERE drinkstore_stock_products.stock_id IS NULL AND receipt_id IN (SELECT receipt_id FROM drinkbook_receiptcomponent JOIN drinkstore_stock_products USING(product_id)))

  3. get recipes (from list_of_related_receipts) which are not in “list_of_incomplete_recipes”

  • 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-15T07:26:44+00:00Added an answer on May 15, 2026 at 7:26 am

    Heh. How stupid am I. This could be solved much easier. I don’t have to find receipes which have at least one component. I can (the same way!) find recipes which i can’t do because there is at least one component which i DON’T have.

    list_of_unavailable_components = ReceiptComponent.objects.exclude(product__in=list_of_available_products).distinct()
    

    And now.

    list_of_available_receipts = Receipt.objects.exclude(receiptcomponent__in = list_of_unavailable_components).distinct()
    

    Simple and clean. Thank you for cooperation ;D

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

Sidebar

Ask A Question

Stats

  • Questions 459k
  • Answers 459k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's nothing special about javascript, you can return it from… May 15, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer Nope, this is not currently possible. You can either modify… May 15, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer Ah! Thankyou Elf King! Your comment about jquery and IE6… May 15, 2026 at 11:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.