﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24804	META API: get_field method cache issue	Aamir Rind	nobody	"When '''Model._meta.get_field''' raises a '''django.core.exceptions.FieldDoesNotExist''' exception for field name, later on if we add the field with that name it stills throws exception because of internal cache issue even the field has been added. Consider below code to reproduce the issue:

{{{
from django.db.models import CharField
from django.core.exceptions import FieldDoesNotExist

class AutoCleanField(CharField):
    def __init__(self, depends_on=None, *args, **kwargs):
        self.depends_on = depends_on
        super(AutoCleanField, self).__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super(AutoCleanField, self).deconstruct()
        if self.depends_on:
            kwargs['depends_on'] = self.depends_on
        return name, path, args, kwargs

    def contribute_to_class(self, cls, name, **kwargs):
        super(AutoCleanField, self).contribute_to_class(cls, name, **kwargs)
        try:
            field = cls._meta.get_field(self.depends_on)
        except FieldDoesNotExist:
            field = None

        if not field:
            raise TypeError('{0} `depends_on` field ""{1}"" does not exists in model ""{2}"".'
                              .format(self.__class__.__name__, self.depends_on, self.model.__name__))

class Test(models.Model):
    auto = AutoCleanField(depends_on='name')  # <--- intentionally setting a field for param `depends_on` which does not exists
}}}

After the above code has been run, now add the '''name''' field in '''Test''' model and re-run, The '''FieldDoesNotExist''' exception does not go away:


{{{
class Test(models.Model):
    # everything should works fine now, but didn't
    name = models.CharField(max_length=255)
    auto = AutoCleanField(depends_on='name')
}}}
"	Bug	closed	Core (Cache system)	1.8	Normal	invalid	meta, meta api, get_field, FieldDoesNotExist, cache, internal cache		Unreviewed	0	0	0	0	0	0
