[PATCHv2 08/10] REST API: make patch.state a string rather than int
Andy Doan
andy.doan at linaro.org
Thu May 19 13:25:30 AEST 2016
On 05/17/2016 08:20 AM, Finucane, Stephen wrote:
>> + def to_representation(self, instance):
>> >+ data = super(PatchSerializer, self).to_representation(instance)
>> >+ # an instance of this object lives across a single http request, so
>> >+ # we can cache patch states once and not do repeated lookups
>> >+ if not getattr(self, 'patch_states_cache', None):
>> >+ self.patch_states_cache = [
>> >+ (x.id, x.name) for x in State.objects.all()]
> I haven't benchmarked this yet, but won't this result in a query on the
> State table every time? How about adding 'state__name' to the queried
> fields and using 'prefetch_related' instead? Alternatively, store the
> cache as part of the class instead (a singleton pattern?).
Both approaches lead to the same number of queries. But
select_related('state__name') is a lot cleaner. Updated in v3.
More information about the Patchwork
mailing list