for the Django-obsessed
Anirudha Bose
Software Engineer @ Legalstart, Paris
https://github.com/onyb
July 2018 @ EuroPython 2018, Edinburgh
Follow this presentation here: https://slides.com/onyb/reobject-ep18
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
price = models.IntegerField()
>>> Book.objects.create(title='The C Programming Language', price=52)
>>> Book.objects.create(title='The Go Programming Language', price=30)
# All books
>>> Book.objects.all()
# Title of books under £50
>>> Book.objects.filter(price__lt=50).values('title')
an ORM layer for Python objects
from reobject.models import Model, Field
class Book(Model):
title = Field()
authors = Field()
price = Field()
>>> # Create a bunch of objects
>>> Book(title='The C Programming Language',
authors=['Kernighan', 'Ritchie'], price=52)
>>> Book(title='The Go Programming Language',
authors=['Donovan', 'Kernighan'], price=30)
>>> Book.objects.all() # All books
[Book(title='The C Programming Language', authors=['Kernighan', 'Ritchie'], price=52),
Book(title='The Go Programming Language', authors=['Donovan', 'Kernighan'], price=30)]
# Title of books under $50
>>> Book.objects.filter(price__lt=50).values('title')
[{'title': 'The Go Programming Language'}, {'title': 'The C Programming Language'}]
# Title of books co-authored by Brian Kernighan
>>> Book.objects.filter(authors__contains='Kernighan').values_list('title', flat=True)
['The Go Programming Language', 'The C Programming Language']
from reobject.models import Model, Field
class Card(Model):
suit = Field()
color = Field()
>>> c1, _ = Card.objects.get_or_create(suit='K', color='♥')
>>> c2, _ = Card.objects.get_or_create(suit='K', color='♥')
>>> c3, _ = Card.objects.get_or_create(suit='A', color='♠')
>>> assert c1 is c2
from reobject import transactional
from reobject.models import Model, Field
class Number(Model):
value = Field()
@transactional
def kaboom(self):
self.value = '1111' # invalid value
self.value += 1 # should rollback
num = Number(value=7)
try:
num.kaboom()
except:
assert num.value == 7 # Mutation to '1111' rolled back