Saturday, May 11, 2013

Re: Trying to figure out ManyToManyField on a legacy database

Hi Brian,

It seems like you don't really have an Many To Many relationship between your "Song" and "Played" objects.  I can see how you would have multiple plays for one song, but considering that the Played model has a track_id, would you ever have multiple songs for a single Play?    It appears that "track_id" is intended to be a foreign key into the Song table, so maybe you could consider using a ForeignKey field to refer to your Song through your Played object.  Django will automatically create 2-way references so you can get to your played objects from your song object::

class Played(models.Model):
  played_id = models.IntegerField(primary_key=True)
  track = models.ForeignKey('Song')
  date_played = models.DateTimeField()
  played_by = models.CharField(max_length=255L)
  played_by_me = models.IntegerField()
  class Meta:
   db_table = 'played'

class Song(models.Model):
  id = models.IntegerField(primary_key=True)
  title = models.CharField(max_length=255L, blank=True)
      class Meta:
          db_table = 'song'

Then, you should be able to get all of the Plays for a particular song::  


s = Song.objects.get(id=1229)
s.title # should be OK still
s.played_set.all() # should be a list of plays.



On Saturday, May 11, 2013 5:33:46 PM UTC-5, Brian Millham wrote:
Hi all,
I'm just trying to learn Django. I have an existing site that I wrote in PHP, and I'l considering converting it to Django,

I have a legacy database that I don't want to make any changes to. I have the basics working in Django, but am having a problem with a ManyToManyField.

Here are the 2 tables (simplified)

class Played(models.Model):
  played_id = models.IntegerField(primary_key=True)
  track_id = models.IntegerField()
  date_played = models.DateTimeField()
  played_by = models.CharField(max_length=255L)
  played_by_me = models.IntegerField()
  class Meta:
   db_table = 'played'

class Song(models.Model):
  id = models.IntegerField(primary_key=True)
  title = models.CharField(max_length=255L, blank=True)
  #These are what I've tried for ManyToMany with the error
  played = models.ManyToManyField(Played, db_column='id') # DatabaseError: (1146, "Table 'ampache.song_played' doesn't exist")
  played = models.ManyToManyField(Played, db_column='track_id') # DatabaseError: (1146, "Table 'ampache.song_played' doesn't exist")
  played = models.ManyToManyField(Played, db_table = 'played', db_column='id') # DatabaseError: (1054, "Unknown column 'T2.song_id' in 'where clause'")
  played = models.ManyToManyField(Played, db_table = 'played', db_column='track_id') # DatabaseError: (1054, "Unknown column 'T2.song_id' in 'where      clause'")
  played = models.ManyToManyField(Played, through = 'played', db_column='id') # AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'
  class Meta:
   db_table = 'song'
The code to produce the errors:
from djmusic.models import Song, Played
s = Song.objects.get(id=1229)
print s.title # Prints the title correctly
print s.played.all() # Generates one of the above errors

I know it's gotta be something simple I'm missing here. If I can get this figured out, there's a good chance that I'll convert my old PHP based site to Django.

Thanks all!
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate