Populating a list in a form based on a relationship

17 views
Skip to first unread message

dimz25

unread,
Feb 13, 2012, 10:15:16 PM2/13/12
to Django users
Hi all,

I'm new to django/python. I have been looking through the docs since
several days but cannot get it to work. This is probably some common
stuff to handle.

Here's the situation:

My app should display a form based on the Report model. A Report has
several fields (reference, na, items) A Report can contain several
items. The items are defined and to be found in the model Item. Till
here I got it working.

Now, the tricky part for me is the following:

A company can make its own item collections. An item collection has a
name, a company (fk to Company model), and a list of items (many to
many relationship to Item model, using a through attribute because
that relation needs to store more information)

So, how can I get the Report form to display the items (in a multiple
select menu) that are part of an item collection. I don't want to
display the whole item list as it is working now.

In the following code you'll find my models.py and views.py:
http://dpaste.com/702880/

I tried to replace the many to many relationship in the report model
to ItemCollection instead of Item, but then it displays the names of
my different ItemCollections which is not the desired result.

To make things clear, a Report is not necesarily related to an
ItemCollection. The ItemCollection is related to a Company. The Report
is made/encoded by a Company. The company selects which items go into
that report.

Based on the company I pass as an argument, the form should get the
list of Items from ItemCollections. I suppose this has to be done in
the view, but I can't get my head around it.

Shawn Milochik

unread,
Feb 13, 2012, 10:56:53 PM2/13/12
to django...@googlegroups.com
You override the __init__ of the form object and set the .choices
property of the form field representing the items.


Reply all
Reply to author
Forward
0 new messages