bbrooke
bbrooke

Reputation: 2347

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

Upvotes: 8

Views: 11135

Answers (5)

tomstan11
tomstan11

Reputation: 1247

If you have two class names in one app make sure you add Single Quotation Marks '' around the name of the referenced class it worked for me.

class foreignClass(models.Model):
    category_id = models.ForeignKey('Product_categories', on_delete=models.CASCADE)

Upvotes: 0

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53990

You have two options:

  1. 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:

  2. You can use a string as the first argument to a ForeignKey or ManytoManyField to specify the model you wish to make the relationship with. This is a django feature. If you look at the documentation for ForeignKey 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'

Upvotes: 4

Glyn Jackson
Glyn Jackson

Reputation: 8354

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?

Upvotes: 14

Jeffrey Froman
Jeffrey Froman

Reputation: 6623

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.

Upvotes: 5

Gandirham
Gandirham

Reputation: 916

Use 'yourapp.Model' format.

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

With quotes.

Upvotes: 2

Related Questions