分享
三行代码  ›  专栏  ›  技术社区  ›  Jayant Jaiswal

ImportError:无法从部分初始化的模块导入名称“Bio”bios.型号'(很可能是由于循环导入)

  •  0
  • Jayant Jaiswal  · 技术社区  · 1 周前

    from django.db import models
    from projects.models import Project
    
    class Bio(models.Model):
     name = models.CharField(max_length=200,null=True,blank=True)
     project = models.ManyToManyField(Project,blank=True)
     number = models.CharField(max_length=11,default=None,null=True)
     text = models.TextField(max_length=280)
    
     def __str__(self):
       return self.name
    

    第二个应用程序模型.py文件

    from django.db import models
    from django.contrib.auth.models import User
    from bios.models import Bio
    from django.conf import settings
     
    class Project(models.Model):
       name = models.CharField(max_length=100)
       owner = models.ForeignKey(User, on_delete=models.CASCADE,null=True)
       bio = models.ManyToManyField(Bio)
       desc = models.TextField(max_length=200)
    
       def __str__(self):
         return self.name
    

    我的目标是创建一个与bio有多对多关系的项目,我希望bio展示这个项目

    1 回复  |  直到 1 周前
        1
  •  1
  •   Biplove Lamichhane    1 周前

    您可以直接从 ManyToManyField

    from django.db import models
    from django.contrib.auth.models import User
    # Here, removed importing Bio 
    from django.conf import settings
     
    class Project(models.Model):
       name = models.CharField(max_length=100)
       owner = models.ForeignKey(User, on_delete=models.CASCADE,null=True)
       bio = models.ManyToManyField('bios.Bio') # Here
       desc = models.TextField(max_length=200)
    
       def __str__(self):
         return self.name