-1

In my installable Django application, I have a class called Timer which I intend for users to implement:

class Timer(models.Model):
  class Meta:
    abstract = True

  project = models.ForeignKey(settings.PROJECT_MODEL)
  user = models.ForeignKey(settings.AUTH_USER_MODEL)

  @abstractmethod
  def finish(self):
    raise NotImplementedError

Additionally, I have a class for recording results:

class TimerResults(models.Model):
  timer1 = models.ForeignKey(settings.TIMER_MODEL)
  parameter1 = models.IntegerField()
  parameter2 = models.IntegerField()
  parameter3 = models.IntegerField()

My plan is for users to create their own Timer, implement finish method, which should then create TimerResults. However, this approach isn't working as intended. When I implement Timer in other app, django try to autodiscover other models, and eventually found TimerResults which has a foreign key referencing settings.TIMER_MODEL. However, TIMER_MODEL is not yet created, resulting in the following error message:

"The field installable_app.TimerResults.timer was declared with a lazy reference to 'app.timer', but app 'app' doesn't provide model 'timer'."

I found a solution where I can make the TimerResults class abstract and force the user to implement it, which solves the problem. However, I'm wondering if there are other ways to solve it.

7
  • How does your "real" Timer looks like? Commented Apr 12, 2024 at 11:24
  • @willeM_VanOnsem, If you're asking about an abstract class, it also contains two foreign keys. Both models are created and updated at the time of makemigrations error. Commented Apr 12, 2024 at 12:17
  • but this is an abstract model, it does not exist, there is no table, you can define a ForeignKey on an abstract model, but not to an abstract model. Commented Apr 12, 2024 at 12:38
  • @willeM_VanOnsem can you please read carefully what I write? It seems you don't get it. Commented Apr 12, 2024 at 14:38
  • @willeM_VanOnsem I mean please read whole question carefully, I clearly said "When I implement Timer in other app, django try to autodiscover other models ..." Commented Apr 12, 2024 at 14:41

1 Answer 1

0

In conclusion, opting for an abstract class appears to be the most suitable approach for my current situation.

Alternatively, considering denormalization by storing results as JSON within the Timer model presents itself as a viable solution.

I also tried using swappable_dependency, but it didn't work because Timer should be already created in initial migration.

However, it's important to note that implementing an abstract base and a concrete model with a relationship to it seems to be impossible due to these constraints.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.