I'm using Django 1.3 but plan toupgrade to 1.4 later this month. I generally return XML which is consumed by a Flex HttpService in the client so I only use the django template to convert a dictionary to XML. My application is strictly intranet with limited data and users so efficiency considerations are minimal. While I'm adequate with Django, I'm far from an expert and I wanted to see if there is a better way to achieve my objective.
In my current application I'm finding a common pattern where I need to do a select_related() and return a list of dictionaries with only a subset of the fields, preferably with simpler names that are typical in the default behavior. Thus in my model, a field name could be "appointment__patient__order__action" while I would prefer the simpler name of "action" be passed to the client. The standard use of the .values(*args) does not meet my needs because it does not allow me to convert field names. I find myself doing a select_related().values() and then later using a for loop to extract/rename the fields of interest. I use the same template to return xml for all queries that return "table-like" data. I am considering defining multiple dictionaries that I would use to rename/extract only the desired fields such as: Mymap = dict(action="appointment__patient__order__action" , fname="appointment__patient__fname", ... Thus I would return a list of dictionaries containing only the fields that had the corresponding dictionary value and the dictionary key would be the one in Mymap using a snippet like... (please ignore capitalization inconsistencies, Outlook just insists on doing this) Values = models.Appointment.objects.select_related().filter(x=1, ...).values() And later Results = [ ] For value in values: Improved = { } For (newname, oldname) in Mymap.items(): Improved[newname] = value[oldname] Results.append(Improved) I see two ways to implement this: a) Since the name map is really specific to the model being used, I would embed it in that model as a class (or perhaps meta) object and also create my own abstract model class that all my "real" models would inherit from so I could embed the mapping inside the model. b) In order to isolate business logic from client api, I could do all the mapping in the views.py class and have the business logic not be aware of which fields were actually required. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.