Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checks.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from itertools import chain
  2. from django.apps import apps
  3. from django.core.checks import Error
  4. def check_generic_foreign_keys(app_configs=None, **kwargs):
  5. from .fields import GenericForeignKey
  6. if app_configs is None:
  7. models = apps.get_models()
  8. else:
  9. models = chain.from_iterable(app_config.get_models() for app_config in app_configs)
  10. errors = []
  11. fields = (
  12. obj for model in models for obj in vars(model).values()
  13. if isinstance(obj, GenericForeignKey)
  14. )
  15. for field in fields:
  16. errors.extend(field.check())
  17. return errors
  18. def check_model_name_lengths(app_configs=None, **kwargs):
  19. if app_configs is None:
  20. models = apps.get_models()
  21. else:
  22. models = chain.from_iterable(app_config.get_models() for app_config in app_configs)
  23. errors = []
  24. for model in models:
  25. if len(model._meta.model_name) > 100:
  26. errors.append(
  27. Error(
  28. 'Model names must be at most 100 characters (got %d).' % (
  29. len(model._meta.model_name),
  30. ),
  31. obj=model,
  32. id='contenttypes.E005',
  33. )
  34. )
  35. return errors