Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django models, one to many relationship with back reference [how]

Tags:

I have a database that I filled with objects using SQLAlchemy, but I cannot figure out how to make the models in Django.

I need the Show objects to have arrays of Episode objects via a relationship. In the database all the Episode objects have a 'show_title' column and all the Show objects have 'episodes'

the objects look like this with SQLAlchemy,

   class AlphaShow(Base) :    
    __tablename__ = 'show' 
    title = Column(UnicodeText, primary_key = True)
    description = Column(UnicodeText)
    image = Column(UnicodeText)
    episodes = relationship("Episode", backref="show")
    def __init__(self,_meta) :
        self.title = _meta.title
        self.image = _meta.image
        self.description = _meta.description

class Episode (Base) : #was without base 
    __tablename__= 'episode'
    title = Column(UnicodeText, primary_key = True)
    link = Column(UnicodeText)
    show_title = Column(UnicodeText, ForeignKey('show.title'))

    def __init__(self, _title, _link) :
        self.title = _title
        self.link = _link

How would I make the models in Django? I tried this but it doesn't work,

class Shows(models.Model) :
    episodes = [] 
    title = models.TextField(primary_key=True)
    description = models.TextField()  
    image = models.TextField()
    class Meta:
        db_table = 'shows'
    def __str__(self):
        return self.title  


class Episode(models.Model) :

    title = models.TextField(primary_key=True)
    link = models.TextField()  
    show_title = models.ForeignKey(Shows)

    class Meta: 
        db_table = 'episodes'
    def __str__(self):
        return self.title 

I guess what I'm missing is, what is the Django equivalent to SQLAlchemy's

episodes = relationship("Episode", backref="show")

Any help would be extremely appreciated thanks!