Django ForeignKey issue related class not found
As per my comment, make sure the class 'Activity' has access to the class 'WorkoutRecord'.
The error means what it says, that WorkoutRecord is not defined in your Activity class.
Check these two things first:
1) Did you import it?
2) Was WorkoutRecord defined before Activity?
The WorkoutRecord class must be defined before (i.e. above) the Activity class, in order to use a reference to the class without quotes. If there are circular references between the classes, then using the quoted string version will work to get a lazy reference to the class defined later in your code.
You have two options:
You can import the
WorkoutRecord
model from whatever module it resides (this is the standard python approach)from myapp.models import WorkoutRecord related_workoutrecord = models.ForeignKey(WorkoutRecord, null=True, blank=True)
Using this approach, sometimes you can get into a situation where you have circular imports, so there is an alternative:
You can use a string as the first argument to a
ForeignKey
orManytoManyField
to specify the model you wish to make the relationship with. This is a django feature. If you look at the documentation forForeignKey
relationships, it says:If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:
This means that if the model hasn't yet been defined, but resides in the same module (i.e. it's below your current code) you can just use the model name in quotes:
'WorkoutRecord'
, but if the model is in another application/module you can also specify that as a string:'myapp.WorkoutRecord'