combobox prepopulated to only related objects to "self"

76 views
Skip to first unread message

Ignacio Soto

unread,
Jul 31, 2012, 6:17:28 PM7/31/12
to django...@googlegroups.com
Hi everyone

i have this model

GROUP and Product


product has a group field thath is foreignkey to GROUP.


and Group has foreignKey to Product to select primary product.

so i want to have a group with many products and one is the "primary product".


so...i could do this by select inline products with "trough" and search an intermediary Model (boolean field).

but it let me select  more than one promary,


the solution is the model this way:.

product has a group field thath is foreignkey to GROUP.


and Group has foreignKey to Product to select primary product.

so i want to have a group with many products and one is the "primary product".

but the primaryproduct field in group will be prepopulated with only the related products to this group.


how i prepopulate the select(combobox) with the reverse query ?






Anton Baklanov

unread,
Aug 1, 2012, 3:50:02 AM8/1/12
to django...@googlegroups.com
you can try something like that:

    group = ...
    choices = [(product.pk, product.name) for product in group.product_set.all()]
    form.fields["myfield"].choices = choices








--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/d3UpfhONEWwJ.
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.



--
Regards,
Anton Baklanov

Ignacio Soto

unread,
Aug 1, 2012, 6:26:17 PM8/1/12
to django...@googlegroups.com
interesting!!! i will give it a try, and let you know.

thanks

2012/8/1 Anton Baklanov <antonb...@gmail.com>



--
__

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