def copy_fields(self, model):
        """
        Creates copies of the fields we are keeping
        track of for the provided model, returning a
        dictionary mapping field name to a copied field object.
        """
        fields = {'__module__' : model.__module__}

        for field in model._meta.fields:

            if not field.name in self._exclude:

                field  = copy.deepcopy(field)

                if isinstance(field, models.AutoField):
                    #we replace the AutoField of the original model
                    #with an IntegerField because a model can
                    #have only one autofield.

                    field.__class__ = models.IntegerField

                if field.primary_key:
                    field.serialize = True

                #OneToOne fields should really be tracked
                #as ForeignKey fields
                if isinstance(field, models.OneToOneField):
                    field.__class__ = models.ForeignKey


                if field.primary_key or field.unique:
                    #unique fields of the original model
                    #can not be guaranteed to be unique
                    #in the audit log entry but they
                    #should still be indexed for faster lookups.

                    field.primary_key = False
                    field._unique = False
                    field.db_index = True


                if field.remote_field and field.remote_field.related_name:
                    field.remote_field.related_name = '_auditlog_{}_{}'.format(
                        model._meta.model_name,
                        field.remote_field.related_name
                    )
                elif field.remote_field:
                    try:
                        if field.remote_field.get_accessor_name():
                            field.remote_field.related_name = '_auditlog_{}_{}'.format(
                                model._meta.model_name,
                                field.remote_field.get_accessor_name()
                            )
                    except e:
                        pass

                fields[field.name] = field

        return fields
def copy_fields ( self , model ) : fields = { '__module__' : model . __module__ } for field in model . _meta . fields : if not field . name in self . _exclude : field = copy . deepcopy ( field ) if isinstance ( field , models . AutoField ) : field . __class__ = models . IntegerField if field . primary_key : field . serialize = True if isinstance ( field , models . OneToOneField ) : field . __class__ = models . ForeignKey if field . primary_key or field . unique : field . primary_key = False field . _unique = False field . db_index = True if field . remote_field and field . remote_field . related_name : field . remote_field . related_name = '_auditlog_{}_{}' . format ( model . _meta . model_name , field . remote_field . related_name ) elif field . remote_field : try : if field . remote_field . get_accessor_name ( ) : field . remote_field . related_name = '_auditlog_{}_{}' . format ( model . _meta . model_name , field . remote_field . get_accessor_name ( ) ) except e : pass fields [ field . name ] = field return fields
Creates copies of the fields we are keeping
        track of for the provided model, returning a
        dictionary mapping field name to a copied field object.