Skip to content

Instantly share code, notes, and snippets.

@amureki
Last active March 25, 2019 08:48
Show Gist options
  • Save amureki/3bfe05bdf28756796506755b8ebff600 to your computer and use it in GitHub Desktop.
Save amureki/3bfe05bdf28756796506755b8ebff600 to your computer and use it in GitHub Desktop.
Python/Django typing and circular dependencies
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'])
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