将扫描文本中的数据作为 YAML 固定装置或 SQL 导入 Django
我正在设置一个简单的 Django 应用程序——一个测验。问题和答案来自经过扫描、OCR 和解析的打印样本测试。我已经将它们解析为 Python 对象,如下所示:
class Quiz(object):
def __init__(self, name):
self.name = name
self.questions = {}
class Question(object):
def __init__(self, text):
self.text = text
self.answers = {}
class Answer(object):
def __init__(self, text, value= -1.0, explanation=""):
self.text = text
self.value = value
self.explanation = explanation
但是我的 Django 模型无法将“列表”作为模型字段使用外键来保持测验之间的关系 -
class Quiz(models.Model):
name = models.CharField(max_length=256)
class Question(models.Model):
quiz = models.ForeignKey(quiz)
order = models.IntegerField()
text = models.TextField()
class Answer(models.Model):
question = models.ForeignKey(question)
order = models.IntegerField()
text = models.TextField()
explanation = models.TextField()
value = models.FloatField()
有人可以建议一种从前者到后者的直接方法吗?谢谢。
I'm setting up a simple Django app -- a quiz. The questions and answers come from printed sample tests that have been scanned, OCR'd and parsed. I've parsed them into Python objects like so:
class Quiz(object):
def __init__(self, name):
self.name = name
self.questions = {}
class Question(object):
def __init__(self, text):
self.text = text
self.answers = {}
class Answer(object):
def __init__(self, text, value= -1.0, explanation=""):
self.text = text
self.value = value
self.explanation = explanation
but my Django models, being unable to have "lists" as model fields use foreign keys to keep the relationship between quiz-
class Quiz(models.Model):
name = models.CharField(max_length=256)
class Question(models.Model):
quiz = models.ForeignKey(quiz)
order = models.IntegerField()
text = models.TextField()
class Answer(models.Model):
question = models.ForeignKey(question)
order = models.IntegerField()
text = models.TextField()
explanation = models.TextField()
value = models.FloatField()
Could someone suggest a straightforward way to go from the former to the latter? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要问自己的第一个问题是为什么要使用这些中间类?这听起来像是糟糕的设计。
一旦我们克服了糟糕的设计。您的代码显示 qustion.answers 是字典而不是列表,我假设您刚刚打错了。
如果您有测验模型实例,则可以调用
Quiz.question_set.all()
来获取该测验的所有问题。此调用的结果将是一个查询集,它是一个可迭代对象,因此调用list(Quiz.question_set.all())
将为您提供一个列表。我无论如何强调你都做错了,很可能应该取消中间人。另外,遵循 PEP8 命名规则是一个非常好的主意,即类的命名如下:
遵循约定会让您的生活更轻松 =)
The first question to ask yourself is why you are using these intermediate classes at all? This sounds like bad design.
Once we move past the bad design. Your code shows that qustion.answers is a dictionary not a list, I assume you just made a typo.
If you have a Quiz model instance you can call
Quiz.question_set.all()
to get all questions for that quiz. The result of this call will be a queryset, which is an iterable object and as such callinglist(Quiz.question_set.all())
will give you a list.I can't emphasize enough though that you're doing it wrong and should most likely just be doing away with the middleman. Also PEP8 naming rules are very much a good idea to follow, ie classes are named like so:
It will make your life easier to follow the conventions =)