Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ForeignKey issue related class not found

I've created the following ForeignKey field in my Model under a class called "Activity"

related_workoutrecord = models.ForeignKey(WorkoutRecord, null=True, blank=True)

The ForeignKey is related to a class called WorkoutRecord which should be allowed to be blank.

After adding this column I ran South and got the following error message:

NameError: name 'WorkoutRecord' is not defined

Any thoughts on what's going on? I've confirmed 'WorkoutRecord' is a class in my model.

Do I need to write WorkoutRecord as a string (with quotes) for example:

related_workoutrecord = models.ForeignKey('WorkoutRecord', null=True, blank=True)

I appreciate the feedback

like image 853
bbrooke Avatar asked Apr 23 '13 20:04

bbrooke


Video Answer


2 Answers

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?

like image 176
Glyn Jackson Avatar answered Nov 15 '22 18:11

Glyn Jackson


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.

like image 38
Jeffrey Froman Avatar answered Nov 15 '22 20:11

Jeffrey Froman