Friday, April 26, 2013

I need help doing a linked lookup in admin

I used the django-contacts project as my starting point for a company contact database.  Its really cool, it keeps "addresses", "phone numbers", etc in seperate db table so you can associate as many as you need to each company or person record.  It uses django.contrib.contenttypes for its relations.  Which is also pretty cool since that lets you attach comments to any other record!

Anyway, I want to build on this by adding an Inventory and Purchasing system.

In my Inventory model I have a field for supplier:

  supplier = models.ForeignKey('contacts.Company', blank=True, null=True)

Which works perfect, you get a pop-up list to pick the lucky vendor from.  Later I might add a filter to limit the choices to a particular type of company - like vendors.  But this is fine for now.

A company record can (and will) have multiple "StreetAddresses" records associated with it through a GenericRelation.  For the PO I want to be able to select one out of that set of addresses.

So for a test, I tried:

  ship_to = models.ForeignKey('contacts.StreetAddress', limit_choices_to={'content_type':27, 'object_id':1, 'location':'shipto'}, blank=True, null=True)

"content_type", "object_id" are the content types fields used to control the Generic Relation.  And those values correspond to pk=1 for records of type "company".  And the "location" field indicates the type of address record.

This works.  I get a list of "ship to" addresses for the company (pk=1).

So, now I want to re-jig this to use the current PO record's setting for "supplier" to automatically limit the address selection.

There might be a way to capitalize on the fact that supplier.street_address  is a GenericRelatedObjectManager seeded with the right values. 

In shell, I read in a PO record with a = PO.objects.get(pk=1)
Then I enter a.supplier.street_address.filter(location="shipto") I get a list of all the "shipto" addresses for the supplier!  Exactly the list I want to be able to pick one from.

But I haven't a clue how to make use of this in the Admin system.  Any ideas appreciated!

I tried making a ForeignKey field to the StreetAddress db, and filter it using values from the supplier record currently in memory:
  ship_to = models.ForeignKey('contacts.StreetAddress', limit_choices_to={'content_type':F('po__supplier__street_address__content_type__id'), 'object_id':F('po__supplier__pk'),}, blank=True, null=True)

In Admin, this returns an empty list of addresses. 

To debug, I went into shell, and tried:

b = StreetAddress.objects.filter(content_type=F('po__supplier__street_address__content_type__id')) 
>>> b

does "F()" write a log someplace that will tell me where this falls apart?  Well, I don't see how it would know what PO record to get "supplier" from.  Or maybe its better to say I don't understand how it determines where to get any of its data from when your walking relations.The Django manual entry for 1.4 is pretty vague on details.

Or, is there a better way to do this?

Thanks in Advance!

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment