Created
January 25, 2024 11:58
-
-
Save mentix02/59d6161d31b51b6723f2c30b6b619109 to your computer and use it in GitHub Desktop.
Some dangerous code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This code contains some extreme levels of black magical Python code with abstractions that | |
your tiny brain just might not be able to comprehend. Read on if you dare to - otherwise | |
leave this file and never return. Consider yourself warned. | |
- mentix02, 2024-01-18 | |
""" | |
from typing import Callable | |
from django.db import models | |
def generate_user_owned_model(*, related_name: str, on_delete: Callable = models.CASCADE) -> type[models.Model]: | |
""" | |
Generates a model that is owned by a user via a foreign key. Use this by inheriting from this - | |
>>> class MyModel(generate_user_owned_model(related_name='my_models')): pass | |
Calling a function which returns a class and inhering from it from another class? That's right. | |
As I said - black magic. | |
""" | |
class _UserOwnerModel(models.Model): | |
user = models.ForeignKey('user.User', related_name=related_name, on_delete=on_delete) | |
class Meta: | |
abstract = True | |
return _UserOwnerModel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment