0

I'm new to Django, and in my current project I have a 'Noun' model with a bunch of 12 parallel-feeling fields: 'nominativeSingular', 'vocativeSingular', 'ablativePlural', etc.

I often seem to want to get the relevant property for a given Noun from some combination of strings ('accusative', 'plural') and while I could handwrite a 12-item dictionary, that seems really horribly inelegant.

The question here: Django: OR queries with dynamic field names suggests using "** dictionary-to-kw-args" - is this indeed what I want here? If so, might it be possible to explain how it might work in this case? Alternatively, is this whole thing really a result of bad database design?

Community
  • 1
  • 1
Greg Pallis
  • 247
  • 2
  • 10

1 Answers1

1

Aha! What I wanted here was getattr, which does exactly this, and is a Python affair rather than a Django one.

This page explained it helpfully: http://effbot.org/zone/python-getattr.htm - essentially, these lines are equivalent:

value = obj.attribute;   

and

value = getattr(obj, "attribute")
Greg Pallis
  • 247
  • 2
  • 10