Catherine Catherine - 2 years ago 64
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

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
template is:

{% load staticfiles %}
{% load rest_framework %}
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href=""/>
<script type="text/javascript" src=""></script>
<title>{{profile.user.first_name}}'s Profile</title>

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)

{{ }}
yields 'None'

{{ }}
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 Source

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>

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

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 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download