views:

101

answers:

2

Is there a way to get the foreign key type of the model my key relates to? Currently, I'm trying something like:

def __init__(self, *args, **kwargs):
        super(JobOrderSupplementForm, self).__init__(*args, **kwargs)
        for field in self.fields:
            if type(self.fields[field]) == TypedChoiceField:
                fieldOption = <Whatever type key points to>.get(id=self.__dict__['initial'][field])
                if not fieldOption.isActive:
                    ...Do something to the choices...


I'm trying to programmatically set the available choices that will be shown in my form. So far, I've only been able to figure out that this snippet below gets me some sort of relation to the Foreign Key object...

self.fields[field].__dict__['coerce']
>>> <bound method ForeignKey.to_python of <django.db.models.fields.related.ForeignKey object at 0x01609EF0>>


Any help would be greatly appreciated.

+1  A: 

Figured it out... it was a very convoluted and tedious process of dir's and type's, but, this line will get me the Model type that the foreign key is related to:

    getattr(type(self.instance), field).field.rel.to
kchau
+1  A: 

Maybe you should look into generic relations?

celopes