views:

286

answers:

2

Hey all, I'm fairly new to both django and jquery, but have a couple years of general oo programming experience (mainly flash/flex/cf), and I'm trying to figure out the best way to implement a form for a sports tournament that manages a few model relationships and uses some simple jquery to improve usability.

My models look like this:

from django.db import models
from django.contrib.auth.models import User
from TTHUltimate.countries.models import Country

# Create your models here.

class BaseItem(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(blank=True, max_length=5000)

    def __unicode__(self):
        return self.name

class Location(models.Model):
    country = models.ForeignKey(Country)
    state_province = models.CharField(max_length = 100)
    city = models.CharField(max_length = 100)

    def __unicode__(self):
        return '%s, %s, %s' % (self.city,self.state_province,self.country)

class Address(models.Model):
    location = models.ForeignKey(Location)
    address = models.CharField(max_length = 500)
    postalCode = models.CharField(max_length = 20)

    def __unicode__(self):
        return '%s, %s %s' % (self.address, self.location.unicode(),self.postalCode)

class Tourney(BaseItem):
    user = models.ForeignKey(User)
    location = models.ForeignKey(Location, verbose_name='Location')

    startDate = models.DateField('Start Date')
    endDate = models.DateField('End Date',blank=True,null=True)

   # webLinks = models.ManyToManyField(WebLinkItem, verbose_name='Web Links')
   # emailContacts = models.ManyToManyField(EmailContactItem, verbose_name='Email Contacts')
   # phoneContacts = models.ManyToManyField(PhoneContactItem, verbose_name='Phone Contacts')
   # addressItems = models.ManyToManyField(AddressItem, verbose_name='Important Addresses')
   # scheduleItems = models.ManyToManyField(ScheduleItem, verbose_name='Schedule')

class TourneyBaseItem(BaseItem):
    tourney = models.ForeignKey(Tourney)

class WebLinkItem(TourneyBaseItem):
    url =  models.URLField()

class EmailContactItem(TourneyBaseItem):
    email =  models.EmailField()

class PhoneContactItem(TourneyBaseItem):
    phone =  models.CharField(max_length=50)

class AddressItem(TourneyBaseItem):
    address = models.ForeignKey(Address)

class ScheduleItem(TourneyBaseItem):
    datetime = models.DateTimeField()

My main issue is how to deal with the Tourney foreign key relationships for the classes at the bottom that extend 'TourneyBaseItem'. I'd like these to be represented in tables that can have items added and deleted through jquery and popups, but there are a few things I'm not sure about:

1.) Did I setup the model relationships correctly to begin with? I also considered the idea of using ManyToMany fields on the Tourney class instead of the Tourney foreign key in item classes, as you can see from the commented out lines. For that matter, I'm also not sure if I took the best approach with the 'location' field.

2.) What is the best way to construct this form in Django? I was hoping to use ModelForm as much as possible to remove the repetition of defining my own fields. I can see passing in ModelForms for the basic data and the item popups, but I'm not sure how it would work with the previously mentioned foreign key relationships. Should those be pieced together in the view?

3.) If I'm using jquery to manage my item lists, how do I then include these lists in the request.POST object? Do I simply run through the data in the tables and add them to hidden select inputs on submission?

Thanks for reading.

-Dane

+1  A: 

It's usually just fine to use ModelForms as is with foreign keys. Django will create list items by default for those fields, and users can just select the appropriate item. If the list generated would be too long (say, with a Users field) you can arrange for the field to be a plain key and use some jquery on the client to make that actually useable.

TokenMacGuy
Thanks, that makes good sense. I see how I can use model forms for the basic tourney data (name, description, start date, end date, etc.) but I'm still a bit unclear on relationships, i.e. foreign keys (for location) and many to manys (for my lists of contacts, links, etc.). I can see how a separate model form would be useful for locations and for adding contacts, links, etc. to lists, but where I'm still foggy is how I tie those in to the main form, and how I would add the data from either a separate form or from dynamic jquery driven lists to the POST data and then save the whole thing.
Dane
A: 

Why do the models for WebLinkItem, EmailContactItem, PhoneContactItem, etc' all inherit from TourneyBaseItem? This means they'll have a FK to tourney. Do you really want that?

I think its better to have M2M relationships as you defined in the lines you commented out. Because in the FK design, you can't use the same WebLinkItem for 2 or more different projects (and it might make sense - a page containing info about several tourneys). Another way to look at it, is that a tourney is just not a part (or a defining attribute) of a WebLinkItem.

Ofri Raviv
Thanks, I think you're right and that's what I was thinking initially. I got myself confused when trying to figure out the best way to set up the form, but it's a mistake to let that effect how I do the models.
Dane