nobrin / macaron

A simple O/R mapper for SQLite
http://nobrin.github.com/macaron/
MIT License
31 stars 8 forks source link

classmethod Model.dummy(**kwargs) #23

Closed FrViPofm closed 10 years ago

FrViPofm commented 11 years ago

Is it possible to have a dummy classmethod for writing such code :

@get("/song/id:int/edit") @get("/song/_new") @view("song.edit.html") def song_edit(id = -1): """Edits song""" if id > 0: try: song = Song.get(id) except Song.DoesNotExist: pass else : song = Song.dummy(lang="fr") return dict(object=song)

This would avoid creating an object in the database if the action is canceled. This would permit to use the same template for creating a new object or editing an existing one.

nobrin commented 11 years ago

Hi, You can write below, If you want to use dummy object for a new object.

try:
    song = Song.get(id)
except Song.DoesNotExist:
    pass
else:
    song = Song(lang="fr")
return dict(object=song)

The constructor of "Song" creates a object which is created on database. It can be use as a new object in the session.

nobrin commented 11 years ago

Sorry, I've mistaken. The constructor of "Song" creates a object which will not be created on the database. The object can be use for a new record on web pages without saving into the database.