I'm having a seemingly simple definition error whereby the class Automation
is not defined. I've created a very simple app called automations
which seems to have caused a problem despite barely changing it. Note that there's also another app called messages
, with a many:many relationship with the new automations.
Automations is just an app with this simple models.py file:
from django.db import models
from accounts.models import Account
from messages.models import Message
class Automation(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=200)
account = models.ForeignKey(Account, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True, null=True)
messages = models.ManyToManyField(Message)
def __str__(self):
return self.name
And messages is a separate app with this models.py file:
from django.db import models
from accounts.models import Account
# from automations.models import Automation # < FAILS
from automations.models import * # < WORKS
# from apps.get_model(email_messages, Message) # < FAILS
# from django.apps get_model(email_messages, Message) # < FAILS
# from django.apps import apps # < FAILS
# Automations = apps.get_model('email_messages', 'Messages') # < FAILS
class Message(models.Model):
name = models.CharField(max_length=100)
subject = models.CharField(max_length=128)
text = models.TextField()
account = models.ForeignKey(Account, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True, null=True)
automations = models.ManyToManyField(Automation) # < PROBLEMATIC LINE
def __str__(self):
return self.name
I initially had this import from automations.models import Automation
which caused this error:
ImportError: cannot import name 'Automation' from partially initialized module 'automations.models' (most likely due to a circular import)
But I no longer received that error after adding from automations.models import *
. However, now I'm getting a definition error NameError: name 'Automation' is not defined
when adding automations = models.ManyToManyField(Automation)
to the Message
class.
Does anyone know why is this happening? I just want to add a many:many relationship between classes Message
& Automation
(presumably a ManytoManyField(Class)
field is needed to be added to both classes?).
I've read other posts with similar issues but could find no solution. Thanks