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