无法从我的数据库访问某些信息与 django 的多对多关系
Having trouble accessing certain information from my database with many to many relationships with django
我正在使用 Django REST Framework 和一个具有多对多关系的 SQL 数据库,这使得在我的应用程序中显示来自数据库的信息变得困难。相关的 table 是:
用户:id,密码,last_login,is_superuser,用户名,first_name,last_name,邮箱,is_staff,is_active , date_joined
个人资料:id,user_id,user_organization_id
组织:id,organization_name
profile_projects:id,profile_id,project_id
项目:id,project_name
我的问题是如何让项目正确显示在我的个人资料页面上。配置文件和项目是多对多的,这就是为什么它们有自己单独的 table。
这是来自models.py
的相关代码
class Project(models.Model):
id = models.AutoField(primary_key=True)
project_name = models.CharField(max_length = 30)
def __str__(self):
return "%s" % self.project_name
class Profile(models.Model):
id = models.AutoField(primary_key=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
projects = models.ManyToManyField(Project)
def __str__(self):
return "%s's profile" % self.user
然后我的 profile.html
模板是:
{% load staticfiles %}
{% load rest_framework %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://bootswatch.com/yeti/bootstrap.min.css"/>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<title>{{profile.user.first_name}}'s Profile</title>
</head>
<body>
<h1>{{profile.user.get_full_name}}</h1>
<h2>{{profile.user_organization.organization_name}}</h2>
<h3>Projects</h3>
{{profile.projects.all}}
<h3>Email</h3>
{{profile.user.email}}
</body>
</html>
我的示例用户 John Doe,他在组织 'Company' 的项目 'fun' 工作,
他的个人资料页面上的所有内容都正确显示,除了项目下它有 [<Project: fun>]
。我已经尝试了很多东西,这些是我得到的结果:
{{ profile.projects }}
在我的模板中产生 'app.Project.None' 在网页上(应用程序是我的 Django 应用程序的名称)
{{ profile.projects.name }}
产量 'None'
{{ profile.projects.all.name }}
没有显示任何内容。所以我知道我很接近但是如何让 [<Project: ___ >]
不出现在项目名称周围?我认为这个问题源于 profile_projects 是多对多的,因为所有其他信息都是一对一或一对多的。
此外,我的示例用户目前每个人只有 1 个项目,一旦我只使用 1 个项目就可以完成所有工作,我将担心为每个配置文件添加和显示多个项目。
当您看到 [<Project: ___ >]
时,您会看到一个项目列表。您需要迭代它们或以某种方式展平它们。
把它变成 <ul>
<ul>
{% for project in profile.projects.all %}
<li>{{ project.name }}</li>
{% endfor %}
</ul>
您可能会发现花时间学习如何在 Python 和 Django 中进行内省是值得的。真的很方便!!我建议安装 ipython
以便 Django 的 manage.py shell
使用起来更友好。
在 shell 提示符下,您可以导入模型并加载示例对象:profile = mymodels.Profile.objects.get.latest('id')
,然后使用 tab
或 dir()
检查对象的属性。
我正在使用 Django REST Framework 和一个具有多对多关系的 SQL 数据库,这使得在我的应用程序中显示来自数据库的信息变得困难。相关的 table 是:
用户:id,密码,last_login,is_superuser,用户名,first_name,last_name,邮箱,is_staff,is_active , date_joined
个人资料:id,user_id,user_organization_id
组织:id,organization_name
profile_projects:id,profile_id,project_id
项目:id,project_name
我的问题是如何让项目正确显示在我的个人资料页面上。配置文件和项目是多对多的,这就是为什么它们有自己单独的 table。
这是来自models.py
class Project(models.Model):
id = models.AutoField(primary_key=True)
project_name = models.CharField(max_length = 30)
def __str__(self):
return "%s" % self.project_name
class Profile(models.Model):
id = models.AutoField(primary_key=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
user_organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
projects = models.ManyToManyField(Project)
def __str__(self):
return "%s's profile" % self.user
然后我的 profile.html
模板是:
{% load staticfiles %}
{% load rest_framework %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://bootswatch.com/yeti/bootstrap.min.css"/>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<title>{{profile.user.first_name}}'s Profile</title>
</head>
<body>
<h1>{{profile.user.get_full_name}}</h1>
<h2>{{profile.user_organization.organization_name}}</h2>
<h3>Projects</h3>
{{profile.projects.all}}
<h3>Email</h3>
{{profile.user.email}}
</body>
</html>
我的示例用户 John Doe,他在组织 'Company' 的项目 'fun' 工作,
他的个人资料页面上的所有内容都正确显示,除了项目下它有 [<Project: fun>]
。我已经尝试了很多东西,这些是我得到的结果:
{{ profile.projects }}
在我的模板中产生 'app.Project.None' 在网页上(应用程序是我的 Django 应用程序的名称)
{{ profile.projects.name }}
产量 'None'
{{ profile.projects.all.name }}
没有显示任何内容。所以我知道我很接近但是如何让 [<Project: ___ >]
不出现在项目名称周围?我认为这个问题源于 profile_projects 是多对多的,因为所有其他信息都是一对一或一对多的。
此外,我的示例用户目前每个人只有 1 个项目,一旦我只使用 1 个项目就可以完成所有工作,我将担心为每个配置文件添加和显示多个项目。
当您看到 [<Project: ___ >]
时,您会看到一个项目列表。您需要迭代它们或以某种方式展平它们。
把它变成 <ul>
<ul>
{% for project in profile.projects.all %}
<li>{{ project.name }}</li>
{% endfor %}
</ul>
您可能会发现花时间学习如何在 Python 和 Django 中进行内省是值得的。真的很方便!!我建议安装 ipython
以便 Django 的 manage.py shell
使用起来更友好。
在 shell 提示符下,您可以导入模型并加载示例对象:profile = mymodels.Profile.objects.get.latest('id')
,然后使用 tab
或 dir()
检查对象的属性。