2011-12-20 10 views
5

Mam ogólną funkcję, która iteruje nad _meta.fields danego obiektu. Wszystkie nazwy pól i wartości są pobierane poprawnie, z wyjątkiem pól ManyToMany. Wydaje się całkowicie ignorować pola ManyToMany. Jak odbieramy fx z pól m2m?wiele pól niewidocznych w _meta.fields

def myfunc(self) 
    for field in self._meta.fields: 
     type = field.get_internal_type() 
     name = field.name 
     val = getattr(self,field.name) 

Odpowiedz

0

Jeśli chcesz, aby wszystkie nazwy pól w modelu. Nie musisz używać self._meta.many_to_many + self._meta.fields.

Możesz po prostu użyć [field.name for field in model._meta.get_fields()].

Zauważ, że get_fields zwróci wszystkie pola (w tym wiele-do-wielu i klucz obcy)

Django get_fields:

def get_fields(self, include_parents=True, include_hidden=False): 
    """ 
    Returns a list of fields associated to the model. By default, includes 
    forward and reverse fields, fields derived from inheritance, but not 
    hidden fields. The returned fields can be changed using the parameters: 

    - include_parents: include fields derived from inheritance 
    - include_hidden: include fields that have a related_name that 
         starts with a "+" 
    """ 
Powiązane problemy