Get name of reverse_fields from model.__dict__

This commit is contained in:
momamene 2016-12-13 13:23:08 +00:00
parent 94bab46a75
commit 8bb55ed79d
2 changed files with 11 additions and 7 deletions

View File

@ -21,8 +21,7 @@ def construct_fields(options):
exclude_fields = options.exclude_fields exclude_fields = options.exclude_fields
fields = OrderedDict() fields = OrderedDict()
for field in _model_fields: for name, field in _model_fields:
name = field.name
is_not_in_only = only_fields and name not in options.only_fields is_not_in_only = only_fields and name not in options.only_fields
is_already_created = name in options.fields is_already_created = name in options.fields
is_excluded = name in exclude_fields or is_already_created is_excluded = name in exclude_fields or is_already_created

View File

@ -30,11 +30,11 @@ def get_reverse_fields(model):
# Hack for making it compatible with Django 1.6 # Hack for making it compatible with Django 1.6
new_related = RelatedObject(related.parent_model, related.model, related.field) new_related = RelatedObject(related.parent_model, related.model, related.field)
new_related.name = name new_related.name = name
yield new_related yield (name, new_related)
elif isinstance(related, models.ManyToOneRel): elif isinstance(related, models.ManyToOneRel):
yield related yield (name, related)
elif isinstance(related, models.ManyToManyRel) and not related.symmetrical: elif isinstance(related, models.ManyToManyRel) and not related.symmetrical:
yield related yield (name, related)
def maybe_queryset(value): def maybe_queryset(value):
@ -45,8 +45,13 @@ def maybe_queryset(value):
def get_model_fields(model): def get_model_fields(model):
reverse_fields = get_reverse_fields(model) reverse_fields = get_reverse_fields(model)
all_fields = sorted(list(model._meta.fields) + all_fields = [
list(model._meta.local_many_to_many)) (field.name, field)
for field
in sorted(list(model._meta.fields) +
list(model._meta.local_many_to_many))
]
all_fields += list(reverse_fields) all_fields += list(reverse_fields)
return all_fields return all_fields