简体   繁体   中英

ImportError: cannot import name 'faker' from partially initialized module 'faker' (most likely due to a circular import)

    1. from django.db import models

       # Create your models here.
    2. class Quiz(models.Model):

       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. import random

    2. import django

    3. import os

    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. return q

    13. def populatequestion(N=10): for entry in range(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,

      explanation=fake_explanation)[0]

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

      if name == ' main ': print("populating script!") populatequestion(10) print("populating populatequestion Complate!")

      `

I run in the same problem before. What you will need to do is add blank=True and null=True``inside of each ForeignKey()` methods.

For instance:

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)
  ...

Bear in mind that the first post will have a null connection, but it can be updated after.

Check the naming of your files, ie if you named a file as faker.py that will cause circular import problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM