Ticket #21081: options.diff

File options.diff, 1.8 KB (added by anonymous, 11 years ago)
  • django/db/models/options.py

     
    44import re
    55from bisect import bisect
    66import warnings
     7import logging
    78
    89from django.conf import settings
    910from django.db.models.fields.related import ManyToManyRel
     
    1516from django.utils.encoding import force_text, smart_text, python_2_unicode_compatible
    1617from django.utils.translation import activate, deactivate_all, get_language, string_concat
    1718
     19logger = logging.getLogger('django.models')
     20
    1821# Calculate the verbose_name by converting from InitialCaps to "lowercase with spaces".
    1922get_verbose_name = lambda class_name: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', class_name).lower().strip()
    2023
     
    121124            if self.verbose_name_plural is None:
    122125                self.verbose_name_plural = string_concat(self.verbose_name, 's')
    123126
    124             # Any leftover attributes must be invalid.
     127            # Any leftover attributes are unknown to Django, and might be typos.
    125128            if meta_attrs != {}:
    126                 raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys()))
     129                # Use difflib to check for close string matches, and warn the
     130                # user if any are found with a greater than 80% confidence.
     131                from difflib import get_close_matches
     132                for unknown_attr in six.iterkeys(meta_attrs):
     133                    matches = get_close_matches(unknown_attr, DEFAULT_NAMES, 1, 0.8)
     134                    if matches:
     135                        logger.warning("Unknown Meta option '%s' found, did you mean '%s'?"
     136                                % (unknown_attr, matches[0]))
    127137        else:
    128138            self.verbose_name_plural = string_concat(self.verbose_name, 's')
    129139        del self.meta
Back to Top