I've run into a problem trying to model something in Django and could
use some advice. In a nutshell, I'm trying to relate a single table
to multiple (other) tables using a single foreign key field and
foreign key type. I'm aware of the manytomany relationship type of
Django but I don't think this will provide the functionality I'm
looking for.
Based on the model below, I have tables named "Artist", "Album" and
"Song". What I'm trying to accomplish is making lists of records from
these tables and assigning a tag to them. This will let me pull a
list by tag name and have access to all of its items. For example, I
might have a Tag named "Legends Of Jazz" that contains 10 tag items.
Some of the tag items point to an Album and some point to an Artist.
Based on the ForeignID field and ItemType relationship, I can create
this list in the database. A problem arises when I try to extract the
list in a view or template. There is no "hard" relationship establish
that I use to get items. If I pull a list of TagItems, how would I
then get the artist or song?
I realize that some readers might question the purpose of creating a
soft relationship like this. The reason is that it allows me to
create new entities in the database and then establish a relationship
without code or database changes. If I decided to start tagging
"Genres" or some other type of entity then I would only need to add a
new ItemType to the database table.
The end goal is to pass a list to my template and format each item
according to its type. I'm actually going to have a view picking tags
at random in order to present different music to the user as they
navigate the site. This way the site appears to be more dynamic and I
don't have to keep adding content. Just create some new Tags.
I would appreciate any advice on how to accomplish this in Django.
Here is my current model:
from django.db import models
import datetime
class Genre(models.Model):
name = models.CharField(maxlength=150)
slug =
models.SlugField(unique=True,prepopulate_from=('name',),maxlength=150)
def __str__(self):
return self.name
class Admin:
pass
class Artist(models.Model):
name = models.CharField(maxlength=150)
slug =
models.SlugField(unique=True,prepopulate_from=('name',),maxlength=150)
def __str__(self):
return self.name
class Admin:
pass
class Album(models.Model):
name = models.CharField(maxlength=150)
slug =
models.SlugField(unique=True,prepopulate_from=('name',),maxlength=150)
publisher = models.CharField(maxlength=100)
releasedate = models.CharField(maxlength=50)
cover = models.CharField(maxlength=300)
description = models.TextField()
rating = models.CharField(maxlength=3)
lookupon = models.DateField(null=True,editable=False)
failedlookup = models.BooleanField(null=True)
modifiedon = models.DateField(editable=False)
artist = models.ForeignKey(Artist,related_name='album_artist')
genre = models.ForeignKey(Genre,related_name='album_genre')
def save(self):
self.modifiedon = datetime.datetime.today()
super(Album, self).save()
def __str__(self):
return self.name
class Admin:
pass
class AlbumReview(models.Model):
Summary = models.CharField(maxlength=100)
Content = models.TextField()
ReviewDate = models.CharField(maxlength=50)
Rating = models.CharField(maxlength=3)
album = models.ForeignKey(Album,related_name='review_album')
def __str__(self):
return self.id
class Admin:
pass
class Song(models.Model):
name = models.CharField(maxlength=150)
slug =
models.SlugField(unique=True,prepopulate_from=('name',),maxlength=150)
bitrate = models.CharField(maxlength=12)
length = models.CharField(maxlength=10)
duration = models.IntegerField()
track = models.IntegerField()
filepath = models.TextField()
album = models.ForeignKey(Album,related_name='song_album')
def __str__(self):
return self.name
class Admin:
pass
class Tag(models.Model):
name = models.CharField(maxlength=100)
slug =
models.SlugField(unique=True,prepopulate_from=('name',),maxlength=150)
displayCount = models.IntegerField()
description = models.TextField(blank=True)
def __str__(self):
return self.name
class Admin:
pass
class TagItemType(models.Model):
name = models.CharField(maxlength=200)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Tag Item Types"
class Admin:
pass
class TagItem(models.Model):
foreignID = models.IntegerField()
description = models.TextField(blank=True)
tag = models.ForeignKey(Tag,related_name='tagItem_tag')
itemType =
models.ForeignKey(TagItemType,related_name='tagItem_type')
def __str__(self):
return ("%s %s ID=%s") %
(self.tag,self.itemType,self.foreignID)
class Meta:
verbose_name_plural = "Tag Items"
class Admin:
pass
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---