mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #12663 -- Formalized the Model._meta API for retrieving fields.
Thanks to Russell Keith-Magee for mentoring this Google Summer of Code 2014 project and everyone else who helped with the patch!
This commit is contained in:
committed by
Tim Graham
parent
749d23251b
commit
fb48eb0581
@@ -6,6 +6,7 @@ and database field objects.
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from collections import OrderedDict
|
||||
from itertools import chain
|
||||
import warnings
|
||||
|
||||
from django.core.exceptions import (
|
||||
@@ -89,7 +90,7 @@ def save_instance(form, instance, fields=None, fail_message='saved',
|
||||
# Note that for historical reasons we want to include also
|
||||
# virtual_fields here. (GenericRelation was previously a fake
|
||||
# m2m field).
|
||||
for f in opts.many_to_many + opts.virtual_fields:
|
||||
for f in chain(opts.many_to_many, opts.virtual_fields):
|
||||
if not hasattr(f, 'save_form_data'):
|
||||
continue
|
||||
if fields and f.name not in fields:
|
||||
@@ -127,7 +128,7 @@ def model_to_dict(instance, fields=None, exclude=None):
|
||||
from django.db.models.fields.related import ManyToManyField
|
||||
opts = instance._meta
|
||||
data = {}
|
||||
for f in opts.concrete_fields + opts.virtual_fields + opts.many_to_many:
|
||||
for f in chain(opts.concrete_fields, opts.virtual_fields, opts.many_to_many):
|
||||
if not getattr(f, 'editable', False):
|
||||
continue
|
||||
if fields and f.name not in fields:
|
||||
@@ -186,7 +187,7 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None,
|
||||
from django.db.models.fields import Field as ModelField
|
||||
sortable_virtual_fields = [f for f in opts.virtual_fields
|
||||
if isinstance(f, ModelField)]
|
||||
for f in sorted(opts.concrete_fields + sortable_virtual_fields + opts.many_to_many):
|
||||
for f in sorted(chain(opts.concrete_fields, sortable_virtual_fields, opts.many_to_many)):
|
||||
if not getattr(f, 'editable', False):
|
||||
continue
|
||||
if fields is not None and f.name not in fields:
|
||||
|
||||
Reference in New Issue
Block a user