Last active
March 25, 2019 08:48
-
-
Save amureki/3bfe05bdf28756796506755b8ebff600 to your computer and use it in GitHub Desktop.
Python/Django typing and circular dependencies
This file contains hidden or 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
from module.models import MainThing | |
class ActivityMixin: | |
is_active = models.BooleanField(default=False) | |
def activate(self: MainThing): | |
# Type definition up there would help PyCharm and other editors | |
# to know that they could access the method of the main class, | |
# BUT it requires an import of the model, which leads to a circular import issue. | |
# How can we solve it? | |
title = self.get_current_title_for_no_reason() | |
self.is_active = True | |
self.save(update_fields=['is_active']) |
This file contains hidden or 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
from django.db import models | |
class MainThing(ActivityMixin, models.Model): | |
"""My very big and complex model, in which I decided to separate some logic into a mixins.py file"""" | |
title = models.CharField(max_length=100) | |
def get_current_title_for_no_reason(self): | |
return self.title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment