在应用之间使用Django模型 [英] Using Django Models across Apps

查看:54
本文介绍了在应用之间使用Django模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,

我见过类似的问题,但没有一个答案能引起我的共鸣。我是Django开发的新手,正在尝试使用这些模型。我的问题是我应该如何适当地利用一个应用程序中另一个应用程序中的模型?

I have seen similar questions but none of the answers really resonate with me. I am new to django development and am trying to get use to the models. My question is how do I utilize a model from one app in another appropriately?

例如,我有一个项目Project 1,该项目由负责管理的Person应用程序组成用户个人资料。该应用程序具有一个人的模型,但随后在活动中使用该人进行他们已经完成的活动以及游戏中的另一个进度。所有这些应用程序都需要人作为关键。

For instance, I have a project, Project 1 that consists of the a Person app that manages the users profile. This app has the model for a person, but then that person is used in Activities for the activities they have completed, and another progress in a game. All of these apps need the person to be the key.

我不知道如何使它们建立联系。

I am not understanding how to make these relate.

感谢您的帮助,因为我成为django-ified 。

Thanks for your help as I become django-ified.

推荐答案

只需将模型导入其他应用程序即可:

Just by importing model to other apps:

from person.models import Person

然后您可以进行任何相关操作例如,具有模型Person的人:

and then you can any relevant thing with model Person, for example:

class Activity(models.Model):
    person = models.ForeignKey(Person)

请确保您导入的模型是 Person 在使用文件之前。
您还可以执行以下操作:

Make sure that you imported model Person in the file before using it. You can also do something like this:

from person.models import Person

def list(request):
    person_list = Person.objects.all()

我希望您了解了如何在 django 的另一个应用程序中使用一种应用程序模型。

I hope you got idea of how to use one app model in another app in django.

您都想使用app_1建模至app_2 models.py views.py 或其他地方,只需导入该模型并根据您的使用您的应用程序中的要求。

Either you want to use app_1 model to app_2 models.py or views.py or somewhere else, just import that model and use it as per your requirement in your application.

这篇关于在应用之间使用Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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