python - ImportError:无法从部分初始化的模块“faker”导入名称“faker”(很可能是由于循环导入)
问题描述
-
从 django.db 导入模型
# Create your models here.
类测验(模型。模型):
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
随机导入
导入 django
导入操作系统
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'schoolauthquiz.settings') django.setup()
fakegen = faker()
name = ['HTML', 'CSS', 'JavaScript', 'MySQL','Python', 'jQuery', 'Bootstrap4','Math']
fake_num_questions = fakegen.randint(1, 10)
def add_quiz():
q = Quiz.objects.get_or_create(
quiz_title=random.choice(name), num_questions=fake_num_questions)[0]
q.save()
返回q
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("填充填充问题完成!")
`
解决方案
我之前遇到过同样的问题。您需要做的是添加blank=True
和null=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)
...
请记住,第一个帖子将有一个空连接,但它可以在之后更新。
推荐阅读
- python - 将广义加法模型应用于 xarray
- javascript - 显示文本的数字文本框
- javascript - Angular observable - 如何从订阅返回 true 或 false 到 observable
- ios - 从 ionic 3 项目构建 ios apk
- macos - 为什么我的 docker build 命令失败
- php - 值不会动态添加到数组中
- c# - 如何使用 C# 反射调用静态泛型类的静态方法?
- c - #define foo 1 ## ## 2 会导致未定义的行为吗?
- python-2.7 - 如何在不到 1.00 秒内从 Zapier python 发送帖子?
- matlab - 如何对单元格进行矢量化操作?