Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #78 (closed defect: fixed)

Opened 14 years ago

Last modified 12 years ago

additional API friendliness for SQLObjects

Reported by: kevin Owned by: anonymous
Priority: highest Milestone: 0.9a4
Component: CherryPy Version: 0.9a4
Severity: minor Keywords:
Cc:

Description

Todd Greenwood wrote this, comparing Django's methods to the ones created by SQLObject. This is specifically related to one-to-many joins, and seems like it can make for a more pleasant api than we have now.

I'm all for clearer syntax, but let me clear up what I was suggesting.

p1 = Page(...)
p1.addEntry(Entry(...)) or p1.addEntry(...)

Not:

p1 = Page(...)
e = Entry(***page = p1***)
p1.addEntry(e)

Specifically, I'm suggesting that SQLObject introspect the added object
at this point, and make the nec primary key to foreign key mapping
automatically:

p1.addEntry(Entry(...)) #Internally, the Entry.page is set to p1 by
SQLObject, not the programmer

As an example of how Django does this sort of thing, here is a trimmed
down example from their web site:

The Django usage of this would be like so:

# Create a Reporter.
>>> r = reporters.Reporter(first_name='John')

<snip>

# Create an Article via the Reporter object.
>>> new_article = r.add_article(headline="John's second story")

http://www.djangoproject.com/documentation/models/many_to_one/

Model source code

from django.core import meta

class Reporter(meta.Model):
   first_name = meta.CharField(maxlength=30)

class Article(meta.Model):
   headline = meta.CharField(maxlength=100)
   reporter = meta.ForeignKey(Reporter)

API reference
Reporter objects have the following methods:

   * add_article()
   * delete()
   * get_article()
   * get_article_count()
   * get_article_list()
   * save()

Article objects have the following methods:

   * delete()
   * get_reporter()
   * save()

-Todd

Change History

comment:1 Changed 14 years ago by kevin

Ian Bicking wrote:

I'm -1 on methods appearing due to other attributes, if it's at all
possible to avoid it.

This would be okay with me, though:

  class Page(SQLObject):
      entries = ManyToOne('Entry')

  p = Page(name='FrontPage')
  p.entries.create(log='why I did this')

Another aspect is that 'add' shouldn't create things, it should just add
things, as you would to a set.  We already have add methods with
RelatedJoin, and they don't create new objects.  Just like sets.Set.add.

This would be a good compliment to demonstrate:

  class Page(SQLObject):
      authors = ManyToMany('User')

  p = Page(name='FrontPage')
  p.authors.add(User.byUsername('ianb'))

Though this could also have a "create()" method.  I don't really like an
add method for ManyToOne, though, since it mutates its argument; but for
ManyToMany it doesn't.

I'm in agreement with Ian on the specifics here. His suggestion for how and where to add additional API goodness is a good one.

comment:2 Changed 13 years ago by ianb@…

  • Status changed from new to closed
  • Resolution set to fixed

Note: this is implemented in the new joins in the SQLObject trunk. It is unlikely to be backported to 0.7.x, though.

comment:3 Changed 13 years ago by Figaro

  • Severity set to minor
  • Component SQLObject deleted
  • Priority changed from high to highest
  • Version changed from 0.9a3 to 0.9a4
  • Milestone changed from 0.9 to 0.9a4
  • Type task deleted

Hi, nice site! But my is better! )))  http://www.mybesthomepage.boom.ru

Note: See TracTickets for help on using tickets.