Amirhossein Nazari
Amirhossein Nazari

Reputation: 45

Circular imports django

I have wrote this code in my models.py file.

As you see the code, I have a class called File and also I have imported in models.py the same class.

Now it gives me this error while migration:

Cannot import name "File" from 'uploadapp.models'

I understand it's the error for circular (recursive) import. But how can I solve this?

from django.db import models
from .models import File

class File(models.Model):
    file = models.FileField(blank=False, null=False)
    def __str__(self):
        return self.file.name

Upvotes: 1

Views: 36

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477883

You remove the from .models import File. It makes no sense to import the module in the same module:

from django.db import models
# from .models import File

class File(models.Model):
    file = models.FileField(blank=False, null=False)

    def __str__(self):
        return self.file.name

Upvotes: 1

Related Questions