Saturday, May 11, 2013

Trying to figure out ManyToManyField on a legacy database

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