Getting my head around django model relationships, forms, and jquery
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 Tour开发者_JAVA技巧ney 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
It's usually just fine to use ModelForm
s 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.
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.
精彩评论