Re: [Django] #14113: Access to extra fields in M2M relations

2010-08-15 Thread Django
#14113: Access to extra fields in M2M relations
---+
  Reporter:  jprafael  | Owner:  nobody 
   
Status:  closed| Milestone: 
   
 Component:  Database layer (models, ORM)  |   Version:  1.2
   
Resolution:  invalid   |  Keywords:  
Many2ManyField intermediary fields
 Stage:  Unreviewed| Has_patch:  0  
   
Needs_docs:  0 |   Needs_tests:  0  
   
Needs_better_patch:  0 |  
---+
Changes (by russellm):

  * status:  new => closed
  * needs_better_patch:  => 0
  * resolution:  => invalid
  * needs_tests:  => 0
  * needs_docs:  => 0

Comment:

 This is already possible -- you just need to think about your data model
 in a slightly different way, and look at the intermediate model as a
 fully-fledged model with a foreign key:

 {{{
 {% for recipe in recipes %}
 {% for rp in recipe.recipeproduct_set.all %}
 product: {{ rp.product.name }} ({{ rp.product.amount }} in
 stock)
 amount: {{ rp.amount }}
 {% endfor %}
 {% endfor %}
 }}}

 Merging the attributes of the intermediate class onto the m2m objects was
 considered at the time the m2m-intermediate feature was under development,
 and was rejected. Search the archives and ticket #6095 for reasoning.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.



[Django] #14113: Access to extra fields in M2M relations

2010-08-14 Thread Django
#14113: Access to extra fields in M2M relations
+---
 Reporter:  jprafael|   Owner:  nobody
   Status:  new |   Milestone:
Component:  Database layer (models, ORM)| Version:  1.2   
 Keywords:  Many2ManyField intermediary fields  |   Stage:  Unreviewed
Has_patch:  0   |  
+---
 I'm using a model similar to
 {{{
 #!python
 class Recipe(models.Model):
 name = models.CharField(max_length = 32, unique=True)
 products = models.ManyToManyField(Product,
 through="RecipeProduct")

 class Product(models.Model):
 name = models.CharField(max_length = 32, unique=True)

 amound = models.IntegerField() # in stock

 class RecipeProduct(models.Model):
 recipe = models.ForeignKey(Recipe)
 product = models.ForeignKey(Product)

 amount  = models.IntegerField()
 }}}
 And need to list all my recipes, along with their products' amounts.
 {{{
 #!python
 Recipe.objects.select_related().all()
 }}}
 Gives access to the list of products in each recipe, but not access to the
 amount field in the intermediary table.

 Perhaps recipe.products.all() should merge the extra fields into the
 Product objects by means of another parameter in the ManyToManyField
 definition where the user can choose the fields to select (and the names
 they would be accessible by) like:
 {{{
 #!python
 products = models.ManyToManyField(Product, through="RecipeProduct",
 extra_fields={'amount': 'recipe_amount'})
 }}}

 This would allow direct usage like:
 {{{
 #!python
 {% for recipe in recipes %}
 {% for product in recipe.products.all() %}
 product: {{ product.name }} ({{ product.amount }} in
 stock)
 amount: {{ product.recipe_amound }}
 {% endfor}
 {% endfor %}
 }}}
 while avoiding name colision problems

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.