Catherine Catherine - 5 months ago 19
MySQL Question

Having trouble accessing certain information from my database with many to many relationships with django

I'm working with Django REST Framework and a SQL database that has some many to many relationships, which is making hard to display information from the database in my app. The relevant tables are:

user: id, password, last_login, is_superuser, username, first_name, last_name, email, is_staff, is_active, date_joined

profile: id, user_id, user_organization_id

organization: id, organization_name

profile_projects: id, profile_id, project_id

project: id, project_name

My problem is with getting projects to display on my profiles page properly. Profiles and projects are many to many, which is why they have their own separate table.

This is the relevant code from

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


And then my
profile.html
template is:

{% 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>


With my sample user John Doe, who works for Organization 'Company' on Project 'fun',
everything on his profile page displays correctly except under Projects it has
[<Project: fun>]
. I've tried a whole host of things and these are the results I'm getting:

{{ profile.projects }}
in my template yields 'app.Project.None' on the webpage (app being the name of my Django app)

{{ profile.projects.name }}
yields 'None'

{{ profile.projects.all.name }}
doesn't make anything show up. So I know that I am close but how do I get the
[<Project: ___ >]
to not show up around the project name? I think the issue is stemming from the fact that profile_projects is many to many because all the other pieces of information are one to one or one to many.

Also my sample users currently only have 1 project each, I will worry about adding and displaying multiple projects per profile once I get everything working with just 1 project.

Answer

When you see [<Project: ___ >], you're seeing a list of projects. You need to iterate over them or flatten them somehow.

Turn this into a <ul>

<ul>
{% for project in profile.projects.all %}
    <li>{% project.name %}</li>
{% endfor %}
</ul>

You might find it worth the time learning how to do introspection in Python and Django. It's really handy!! I recommend installing ipython so that Django's manage.py shell is much friendlier to use.

At the shell prompt you can import your models and load a sample object: profile = mymodels.Profile.objects.get.latest('id') and then use tab or dir() to examine the object's attributes.