如何在Django中将模型从一个应用程序导入到另一个应用程序? [英] How to import models from one app to another app in Django?

查看:80
本文介绍了如何在Django中将模型从一个应用程序导入到另一个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在另一个应用程序的views文件中引用一个应用程序中的模型(人).不幸的是,我收到未解决的参考"错误.是否可以从Django中的其他应用程序引用模型?如果是这样,我在做什么错了?

I am trying to reference a model (Person) from one app, in the views-file of another app. Unfortunately, I get an "unresolved reference"-error. Is it possible to reference models from other apps in Django? If so, what am I doing wrong?

让我用一个例子来演示:

Let me demonstrate with an example:

下图显示了我的项目.我目前在名为自动完成"的应用程序中的views.py(以绿色标记)中.我想在文件"models.py"(用红色标记)中引用一个人模型,该模型属于应用程序"resultregistration".但是,即使在模型中确实存在Person类,也会收到错误消息"Unresolved reference Person".

The image below shows my project. I am currently in the views.py (marked in green), in the app called "autocomplete". I want to reference a person-model in the file "models.py" (marked in red), that belongs to the app "resultregistration". However, I get the error "Unresolved reference Person", even though the class Person does exist in models.py

文件settings.py位于athlitikos/athlitikos-文件夹中,manage.py仅位于athlitikos中(如下图所示)

The file settings.py is in the athlitikos/athlitikos - folder, and manage.py is in only athlitikos (as seen in the image below)

任何帮助将不胜感激!

编辑:我现在尝试从..resultregistration.models import Person"运行,因为我发现屏幕快照中的操作显然是错误的.但是,然后我收到错误消息尝试相对顶级包进行相对导入"

I now tried running "from ..resultregistration.models import Person", because I saw that what I did in the screenshot was obviously wrong. However, then I get the error message " attempted relative import beyond top-level package"

谢谢您的时间!

推荐答案

如果 resultregistration 应用程序位于项目目录(包含 manage.py 的应用程序)中,则您导入中完全不应包含项目名称 athlitikos .请尝试以下操作:

If the resultregistration app is in the project directory (the one containing manage.py) then you shouldn't include the project name athlitikos in the import at all. Try the following:

from resultregistration.models import Person

这篇关于如何在Django中将模型从一个应用程序导入到另一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆