Search code examples
djangopython-2.7django-modelsdjango-1.7django-1.8

Iterating over Django model attributes


I have a class defined like this:

class MyModel(models.Model):
...

I have an instance of this class and I would like to iterate over it's meta attributes:

opts = instance._meta
for f in opts.many_to_many + opts.concrete_fields + opts.virtual_fields:
    ...

This code works perfectly with Django 1.7, but doesn't work with Django 1.8. The error I'm receiving is a type error and it's value is: can only concatenate tuple (not "list") to tuple

What can be the problem with it?


Solution

  • The problem is in the opts.virtual_fields type. It is list now. I guess it was changed in 1.8

    To solve it:

    metas = opts.concrete_fields + opts.many_to_many + tuple(opts.virtual_fields)