Commits

Ralph Bean committed 5c7a727

Your first DB model.

  • Participants
  • Parent commits 21aab0b

Comments (0)

Files changed (3)

+*.db
+*.pyc
+import elixir, tw2.sqla
+elixir.session = tw2.sqla.transactional_session()
+elixir.metadata = elixir.sqlalchemy.MetaData('sqlite:///myapp.db')
+
+class Movie(elixir.Entity):
+    title = elixir.Field(elixir.String)
+    director = elixir.Field(elixir.String)
+    genre = elixir.ManyToMany('Genre')
+    cast = elixir.OneToMany('Cast')
+
+class Genre(elixir.Entity):
+    name = elixir.Field(elixir.String)
+    def __unicode__(self):
+        return self.name
+
+class Cast(elixir.Entity):
+    movie = elixir.ManyToOne(Movie)
+    character = elixir.Field(elixir.String)
+    actor = elixir.Field(elixir.String)
+
+elixir.setup_all()
 import tw2.core
 import tw2.forms
+import tw2.sqla
+import model
 
 class Index(tw2.core.Page):
     template = 'genshi:./index.html'
     def fetch_data(self, req):
         self.req = str(req)
 
-class Movie(tw2.forms.FormPage):
+class Movie(tw2.sqla.DbFormPage):
+    entity = model.Movie
     resources = [tw2.core.CSSLink(filename='myapp.css')]
     title = 'Movie'
     class child(tw2.forms.TableForm):
         title = tw2.forms.TextField(validator=tw2.core.Required)
         director = tw2.forms.TextField()
-        genre = tw2.forms.CheckBoxList(
-            options=['Action', 'Comedy', 'Romance', 'Sci-fi'])
+        genre = tw2.sqla.DbCheckBoxList(entity=model.Genre)
         class cast(tw2.forms.GridLayout):
             extra_reps = 5
             character = tw2.forms.TextField()
             actor = tw2.forms.TextField()
 
 
-tw2.core.dev_server()
+tw2.core.dev_server(repoze_tm=True)