Robert Johnstone
Robert Johnstone

Reputation: 5371

Looping through models in Django and getting column names and .get_FOO_display by default

I have the following:

# model

TITLE_CHOICES = (
    ('mr', 'Mr.'),
    ('ms', 'Ms.'),
    ('mrs', 'Mrs.'),
    ('mis', 'Miss.'),
)
class Client(models.Model):
    name_title = models.CharField(max_length=3, choices=TITLE_CHOICES)
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=40)

# form
class ClientForm(ModelForm):
    class Meta:
        class = Client

# view
def client_view(request):
    client = Client.object.get(id=1)
    clientForm = ClientForm(instance=client)
    return render_to_response('client.html',{'client':client,
                                             'clientForm':clientForm}, ...)

# client.html
...

How can I loop through the object client printing out the column name and the value while making sure that if the value is a choice it prints out the human-readable choice value, not the stored value (get_title_display)?

And why is this not eaiser to do in Django? (isn't this a common thing to want do?)

If I can't do this I have to go statically through each column and use get_title_display, which means that there is no separation between model and template, which means if I change my model I have to manually update the template(s). This is not good

Upvotes: 0

Views: 333

Answers (2)

miki725
miki725

Reputation: 27861

If you want to get get_FOO_display by default, you have to overwrite the __getattribute__ method. Try something like this:

class FooModel(models.Model):
    ...
    def __getattribute__(self, item):
        get = lambda i: object.__getattribute__(self, i)
        name_map = get('_meta')._name_map

        if item.startswith('_') or name_map.has_key(item):
            return get(item)

        else:
            field = name_map.get(item)
            if field.choices:
                return get('get_%s_display' % item)()

            else:
                return get(item)

Upvotes: 0

JamesO
JamesO

Reputation: 25956

Try something like:

# add to your Client model    
def get_fields(self):
    fields_display = []
    for f in Client._meta.fields:
        name = f.name        
        if len(f.choices) == 0:
            fields_display.append([name, f.value_to_string(self)])
        else:
            fields_display.append([name, getattr(self,"get_%s_display" % name)()])

   return fields_display

You can then loop over get_fields in your template for a given object

Upvotes: 1

Related Questions