Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Like(models.Model):
- TARGET_MODEL = 'TargetModel'
- user = models.ForeignKey(settings.AUTH_USER_MODEL)
- target = models.ForeignKey(TARGET_MODEL)
- class Meta:
- abstract = True
- class LikeForPost(Like):
- TARGET_MODEL = 'blog.Post'
- class Like(models.Model):
- TARGET_MODEL = None # will be overridden in subclass
- user = models.ForeignKey(settings.AUTH_USER_MODEL)
- @classmethod
- def on_class_prepared(cls):
- target_field = models.ForeignKey(cls.TARGET_MODEL)
- target_field.contribute_to_class(cls, 'target')
- class Meta:
- abstract = True
- class LikeForPost(Like):
- TARGET_MODEL = 'blog.Post'
- from django.apps import AppConfig
- from django.db.models import signals
- def call_on_class_prepared(sender, **kwargs):
- """Calls the function only if it is defined in the class being prepared"""
- try:
- sender.on_class_prepared()
- except AttributeError:
- pass
- class BlogConfig(AppConfig):
- name = 'blog'
- def __init__(self, app_name, app_module):
- super(BlogConfig, self).__init__(app_name, app_module)
- # Connect programmatic class adjustment function to the signal
- signals.class_prepared.connect(call_on_class_prepared)
- default_app_config = 'blog.apps.BlogConfig'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement