首页 > 解决方案 > ImportError:无法从部分初始化的模块“faker”导入名称“faker”(很可能是由于循环导入)

问题描述

    1. 从 django.db 导入模型

        # Create your models here. 
      
    2. 类测验(模型。模型):

          quiz_title = models.CharField(max_length=300)
          num_questions = models.IntegerField(default=0)
      
          def __str__(self):
              return self.quiz_title
      
      
      class Question(models.Model):
          quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
          question_text = models.TextField(max_length=10000)
          question_num = models.IntegerField(default=0)
          answer = models.CharField(max_length=300)
          explanation = models.TextField(max_length=5000)
      
          def __str__(self):
              return str(self.question_num)
      
      
      
      
      
      class Choice(models.Model):
          question = models.ForeignKey(Question, on_delete=models.CASCADE)
          choice_text = models.CharField(max_length=300)
          correct = models.BooleanField(default=False)
      
          def __str__(self):
              return str(self.choice_text)
      

    from random import randint 2. from faker import faker 3. from quiz.models import Quiz, Question, Choice

    1. 随机导入

    2. 导入 django

    3. 导入操作系统

    4. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'schoolauthquiz.settings') django.setup()

    5. fakegen = faker()

    6. name = ['HTML', 'CSS', 'JavaScript', 'MySQL','Python', 'jQuery', 'Bootstrap4','Math']

    7. fake_num_questions = fakegen.randint(1, 10)

    8. def add_quiz():

    9. q = Quiz.objects.get_or_create(

    10. quiz_title=random.choice(name), num_questions=fake_num_questions)[0]

    11. q.save()

    12. 返回q

    13. def populatequestion(N=10): 输入范围(N): quiz = add_quiz()

          fake_question_text = fakegen.question_text()
          # fake_question_num = fakegen.question_num()
          fake_answer = fakegen.answer()
          fake_explanation = fakegen.explanation()
      
          # fake_question = fakegen.question()
          fake_choice_text = fakegen.choice_text()
          fake_correct = fakegen.correct()
      
          Q = Question.objects.get_or_create(quiz=quiz, question_text=fake_question_text,
                                             question_num=fake_num_questions, answer=fake_answer,
      

      解释=假解释)[0]

          ch = Choice.objects.get_or_create(
              question=Q, correct=fake_correct, choice_text=fake_choice_text)[0]
      

      if name == ' main ': print("填充脚本!") populatequestion(10) print("填充填充问题完成!")

      `

标签: pythondjango

解决方案


我之前遇到过同样的问题。您需要做的是添加blank=Truenull=True``inside of each ForeignKey()` 方法。

例如:

class Question(models.Model):

    """
    The Quiz models is defined as string.
    Since it is declared below this model.
    """
    quiz = models.ForeignKey('Quiz', on_delete=models.CASCADE, null=True, blank=True)
    ...


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE,  null=True, blank=True)
  ...

请记住,第一个帖子将有一个空连接,但它可以在之后更新。


推荐阅读