Doug,

Thank you so much.  I completely missed that detail while ripping
through the django docs.  I've got it all working now.  Ramiro, thanks
again for your help.

For anyone else who is trying to do this and stumbles upon this
thread, here is my full final solution.

--- Overview ---
I've got two applications within the scinet project.
scinet.conferences and scinet.scientists  There are 4 files in all
that I need for this to work.  scinet.conferences.models.py,
scinet.scientists.models.py, scinet.scientists.views.py and
scinet.templates.scientists.portal.html

--- scinet.conferences.models (DjangoProjects/scinet/conferences/
models.py) ---

I define the many-to-many relationship between conferences and
scientists here.  A conference can be attended by multiple scientists
and a scientist can attend multiple conferences.  Do note that I have
arbitrarily chosen to use the Conference model to specify the
relationship between Conferences and Scientists.  I could have done
this in my scientist model just as well.  It is important to not that
I had to import the Scientist model here since I must reference it in
order to specify the relationship in the following line: scientists =
models.ManyToManyField(Scientist, through='ConferenceAttendee').

from django.db import models
from scinet.scientists.models import Scientist

class Conference(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    date = models.DateTimeField()
    scientists = models.ManyToManyField(Scientist,
through='ConferenceAttendee')
    created = models.DateTimeField()
    modified = models.DateTimeField()

    def __unicode__(self):
        return self.name

class ConferenceAttendee(models.Model):
    conference = models.ForeignKey(Conference)
    scientist = models.ForeignKey(Scientist)
    rsvp_date = models.DateTimeField()
    presenter = models.BooleanField()

--- scinet.scientists.models (DjangoProjects/scinet/scientists/
models.py) ---

Here is where I define my Scientist model.  The point to note here is
that I do NOT need to reference the conference model because in
Django, I only need to define a relationship between two models in one
of the two models.  In fact, you must ONLY define the relationship
between two models in ONE of the two models.  DO NOT SPECIFY A
RELATIONSHIP BETWEEN TWO MODELS IN BOTH MODELS.

from django.db import models

class Scientist(models.Model):
    user = models.ForeignKey('auth.User')
    summary = models.TextField()
    undergrad = models.CharField(max_length=50)
    graduate = models.CharField(max_length=50)
    post_doctorate = models.CharField(max_length=50)
    current_employer = models.CharField(max_length=50)
    zipcode = models.CharField(max_length=5)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=2)
    country = models.CharField(max_length=200, blank=False)
    created = models.DateTimeField()
    modified = models.DateTimeField()

--- scinet.scientists.views (DjangoProjects/scinet/scientists/
views.py) ---

There is nothing special to note here other than that I am simply
getting a scientist.  The following line grabs a scientist and all
related conferences.  s = get_object_or_404(Scientist,
pk=scientist_id).  In the template file which I will show next, you
will see how I access the conferences that are associated with this
particular scientist.  Do note once again that there is no need to
explicitly import the Conference model.  Django (or Python) does this
magically (which I am curious about if anyone is up for explaining
that to me).

from django.shortcuts import render_to_response, get_object_or_404
from django.template import Context, loader
from scinet.scientists.models import Scientist
from django.http import HttpResponse, HttpResponseRedirect

def portal(request):
    scientist_id = 1
    s = get_object_or_404(Scientist, pk=scientist_id)
    return render_to_response('scientists/portal.html',
{'scientist':s})

--- scinet.templates.scientists.portal (DjangoProjects/scinet/
templates/scientists/portal.html) ---

The key here is to understand how to traverse related models in
reverse.  Check out the link that Doug posted above.  In short,
because I chose to indicate a relationship between Conferences and
Scientists in the Conference model, Scientist must access conferences
in reverse.  I used Django's default naming of a reverse model
reference in my loop below.  In case you don't know, here is the loop
for traversing scientists that belong to a conference.

                                    {% for scientist in 
conference.scientists.all %}
                                        <li><a href="#">{{ 
scientist.user.get_full_name }}</a></li>
                                    {% endfor %}

In order to traverse the conferences that a scientist will attend you
must use scientists.conference_set.all.  The key differnece being
"_set".  Anyways, the template code that I used is below.

<h1>{{ scientist.user.get_full_name }}</h1>
<h2>You are attending the following conferences</h2>

{% for conference in scientist.conference_set.all %}
    <a href="/">{{ conference.title }}</a>,
{% endfor %}

Thanks again everyone.  I hope that this helps someone else.

Thank you,

Guy

On Apr 5, 1:08 pm, Doug B <dball...@gmail.com> wrote:
> Take a look at this part of the documentation:
>
> http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-rel...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to