P
P
pzverr2014-11-21 14:28:57
Django
pzverr, 2014-11-21 14:28:57

How to get the field names of a Django 1.6 model?

Good afternoon!
There are 2 classes:

class ParentClass(model.Models):
...

class ChildClass(ParentClass):
...

You need to get the names of the fields of the child class.
did so
return [(obj._meta.get_field(field.name)) for field in obj._meta.fields]

But it also returns the fields of the inherited class.
It's decided.
def get_only_child_fields(obj):
        allfields = [(field.name) for field in obj._meta.fields]
        parentfields = [(field.name) for field in Product._meta.fields]
        return [f for f in allfields if f not in parentfields]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2014-11-21
@Masik

It's ugly in my opinion, but still:

fields = obj._meta.fields
child_fields = filter(lambda x: '.ChildClass.' in x.__str__(), fields)
parent_fields = filter(lambda x: '.ParentClass.' in x.__str__(), fields)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question