Here is the essentials from my models.py file:
--------------------------------------------------------------------------------------------------------
class Event (models.Model):
        contact = models.CharField(max_length=100)
        email = models.CharField(max_length=100)
        url = models.CharField(max_length=100)
        title = models.CharField(max_length=100)
        start = models.DateTimeField('date time when')
        end = models.DateTimeField('date till')

class Location (models.Model):
    name = models.CharField(max_length=50)
    state = models.ForeignKey(State)
    location = models.ForeignKey(Event)
--------------------------------------------------------------------------------------------------------
This is a One-to-Many Relationship where an Event can have one or more
Locations.

The problem i need to solve is in the 'views.py' I want to be able to
query 'Event' such that i retrieve an arbitrary subset of events AND
any associated Locations and return only the relevant fields to the
template layer.

The most standard use for this database will be to get all events from
today into the future and their associated locations. On average there
would be say 25 event rows returned and perhaps 27 locations as some
events have more than one location.
--------------------------------------------------------------------------------------------------------
e_future = Event.objects.filter(start__gte = datetime.today())
--------------------------------------------------------------------------------------------------------
having read http://docs.djangoproject.com/en/dev/topics/db/queries/

i realise i can use a reverse foreign key to access an item in the
Location table:
--------------------------------------------------------------------------------------------------------
for e in e_future:
    e_location=e.location_set.all()
--------------------------------------------------------------------------------------------------------
>From here it gets messy as i have to deal with the individual event
elements 'e' out of a queryset 'e_future' and then reassemble the
elements back into a queryset *i dont know how to do this* so my
method can return the selected columns:
--------------------------------------------------------------------------------------------------------
e_future_subset = e_future.values('start', 'title','id',)
e_location_subset = e_location.values('state_id', 'name',)
--------------------------------------------------------------------------------------------------------
Then i have to combine the two objects 'e_future_subset' and
'e_location_subset' (which i believe are dictionaries) and return the
result.
--------------------------------------------------------------------------------------------------------
e_all = e_future_subset + e_location_subset
return render_to_response('events.html', { 'e_all': e_all,})
--------------------------------------------------------------------------------------------------------

As you can see I have been researching the solution and i'm stuck only
on a couple of points.
Surely this is one of the most common requirements for a django setup
where the 'model.py' describes a number of related tables 'one-to-
many' 'one-to-one' or 'many-to-many' relationships, a query (or number
of queries) are made and results are returned from multiple tables
based on an entry's 'id' and accessed through foreign-key pointers.

Any help would be greatly appreciated as I seem to have a handle on
everything else in my django project.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to