I'm passing company information and category information to a Django template page and I'd like to take a value from Company.companycategory and look up this id in another model CompanyCategory which returns a human readable string. Is there a way to do this within the template?

So company.key.id is the id which I can use to look up records from the CompanyCategory model and I'm sending a list of categories. So I guess my only choice is to loop trough all categories looking for a match? Or is there a better way to do this?

2 Answers
2

If you were using vanilla django it seems like your models are related enough to warrant a ForeignKey relationship in which case you could just access the company category by something like company.companycategory.name

If you can't create a reference property then I believe you will just have to look up the corresponding category in your view for each company or You can create a method on your model that will fetch this data for you, especially since this seems like it could be a common operation. Decorating the method using @property should allow you to access it inside your templates.

Thanks. I have tried using company.companycategory.name to get the name within the template i.e {{ company.companycategory.name }}, but I have not been able to get the name associated. I'm off now to look at decorators..
–
AndroidianMay 28 '12 at 11:43

This is what I have in my Company class: code @property def catname(self): return self.companycategory.name companycategory = db.ReferenceProperty(CompanyCategory, collection_name='compcategory') code But I cannot seem to get the name of the category with the template as code{{company.catname}}code
–
AndroidianMay 28 '12 at 12:52

@Androidian I believe you access it through collect_name which you set to compcategory and not the fieldname (companycategory). Could you try company.compcategory.categoryname. Also it wouldn't be name it would be categoryname (because that is what you defined in your CompanyCategory model. So either company.compcategory.categoryname or company.companycategory.categoryname one of those should work~
–
dm03514May 28 '12 at 13:58

I have this working now with {{company.companycategory.categoryname}}. Thanks
–
AndroidianMay 28 '12 at 15:12