Skip to main content

A minimal ORM for postgres

Project description

orm.py
========

A small ORM for Python.

Features
--------

- Basic queries (select, insert, update, order by, limit, offset)
- Simple model interace with foreign keys


Not Features
------------

- Database or table creation, modification, or migration
- Joins, full text search, most other database features


Requirements
------------

- python3
- postgresql
- psycopg2


To do
-----

- python2 support
- better comparison support
- only update fields that have changed
- order by with `get`
- query unique entries
- tests
- improve query interface
- expand sql functionality


Example
===

Tables are not created for you, so you must create them yourself.
Table names should match the class name when converted to underscore-
separated words, so a class UserRelationship will map to the table
"user\_relationship".

Tables must have a serial primary key called "id", and foreign key
fields must be appended with "\_id".

import orm

''' You should first set up tables named "user" and "message" '''
class User(orm.Model):
username = orm.Field()
password = orm.Field()

def get_all_received_messages(self):
''' You could also pass in a limit and offset, or yield results
one at a time.
'''
return list(Message.all(user_to=self, order_by='-date_sent'))

def get_all_sent_messages(self):
return list(Message.all(user_from=user, order_by='-date_sent'))

class Message(orm.Model):
user_from = orm.ForeignKey(User) # field should be named "user_from_id"
user_to = orm.ForeignKey(User) # field should be named "user_to_id"
date_sent = orm.Field()
body = orm.Field()
read = orm.Field(default=False)

Now you can make queries in a script or interpreter:

u = User.get(id=1)
m = list(Message.all(user_from=u))

API
---

user.save()

Saves or updates a Model object.

user.delete()

Deletes the Model object. Any cascades must be done in table definitions.


Model.get(id=None, **kwargs)

Pass in the ID or kwargs matching your model fields.

Model.all(separator='AND', order_by=None, limit='ALL', offset=None, **kwargs):

`all` yields the results, so you must catch them in a loop or list.

- separator: pass in 'OR' and a kwarg with a list of possible arguments. For example:


Message.all(separator='OR', user_from=[user_1, user_2])

This will match all messages from either user\_1 or user\_2.

- order\_by: pass in a string with a field name. Prepend '-' to reverse the order.

- limit: Limit results to first n results.

- offset: Integer to offset your search result

- kwargs: each kwarg value can be a single object or a list.


Model.count(**kwargs)

Returns an integer.

Model.is_unique(**kwargs)

Returns True or False.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

orm.py-0.1.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

orm.py-0.1-cp34-cp34m-macosx_10_6_x86_64.whl (4.4 kB view details)

Uploaded CPython 3.4mmacOS 10.6+ x86-64

File details

Details for the file orm.py-0.1.tar.gz.

File metadata

  • Download URL: orm.py-0.1.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for orm.py-0.1.tar.gz
Algorithm Hash digest
SHA256 fe479cd24aca7edf65c233170998ff03ff3cbc908e22a402e450a886336432c2
MD5 206bb5b2efa95aebae1563a3d8f9dfb2
BLAKE2b-256 4bf268ad02a6e7ae31e7885e43e908c0cba7829d0fb82e6a69fcc9f1ae0dcd42

See more details on using hashes here.

File details

Details for the file orm.py-0.1-cp34-cp34m-macosx_10_6_x86_64.whl.

File metadata

File hashes

Hashes for orm.py-0.1-cp34-cp34m-macosx_10_6_x86_64.whl
Algorithm Hash digest
SHA256 4377945428c9e3c2a7690b44646745ef7556967b1b1cc38e04142c7c73cff9bf
MD5 f235ee0e328df6ebd71e0965c875f78b
BLAKE2b-256 9af05568b49dcdecab47a475569c210dfa854e733be7ad668b4567191f5737a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page