ManyToMany relationship with intermediate model and admin list display with each realtion with value as diferent column

135 views
Skip to first unread message

Ignacio Soto

unread,
Jul 24, 2012, 12:44:47 PM7/24/12
to django...@googlegroups.com
Hey guys

i have an issue, i don know if it really can be done.

im using django 1.3

i have a model producto(product) and Tienda(store)

and a product could be in one or more stores, so i created a many to many relationship but y have a stock in each store, so i use the intermediary model ProductStore.

i want to create this, in the admin , product's  list display, i want to show each manutomany relationship as a diferent column qith the stock as the value.



as shown in the picture, this i could do by add a method in the model getTienda1 and return the stock and do it for each store, but i want it to be created dinamically because the user could create a new store and it will not be there.

so this is my cuestion: can i do that?

(Sorry for my bad english)

Tomas Neme

unread,
Jul 24, 2012, 1:05:24 PM7/24/12
to django...@googlegroups.com

> as shown in the picture, this i could do by add a method in the model
> getTienda1 and return the stock and do it for each store, but i want it to
> be created dinamically because the user could create a new store and it will
> not be there.
>
> so this is my cuestion: can i do that?

Interesting question. You COULD try the following:

in the ProductAdmin ModelAdmin's class' __init__ you can define list_display like

from store.models import Tienda
for store in Tienda.objects.all():
    self.list_display.append('stock_tienda_{id}'.format(id=store.id))

this would give you the list of existing stores at server init time.
and then you could define your Product model's __getattr__ method something like this:

def __getattr__(self, attr):
     base = 'stock_tienda_'
     if attr[:len(base)] == base:
         return Product.objects.filter(tienda=Tienda.objects.get(id=attr[len(base):]))
     return super(Product, self).__getattr__(self, attr)

this should make the ModelAdmin's hack work

finally, you need a way to add new columns for newly created stores, so maybe a post_save signal on Tienda objects

@receiver(post_save, sender=Tienda)
def add_to_product_list_display(sender, instance, new, **kwargs):
      if new:
          # I don't know how to get the ModelAdmin's instance for a given class,
          #  but it must be something in django.contrib.admin.site
          product_admin.list_display.append('stock_tienda_{id}'.format(id=instance.id))

you probably need another listener before .delete of stores, and I'm not certain the __getattr__ hack will work, but this sounds interesting.

Please try on those lines, and let me know if this worked, I'm most interested

--
"The whole of Japan is pure invention. There is no such country, there are
no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

Ignacio Soto

unread,
Jul 24, 2012, 1:10:43 PM7/24/12
to django...@googlegroups.com
ok, it looks very nice i will try and let you know.. thanks

2012/7/24 Tomas Neme <lacry...@gmail.com>

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



--
__

Ignacio Soto Reveco
Staff IngeHost
Reply all
Reply to author
Forward
0 new messages