Inverted `ModelAttribute`

Today when two models reference each other, we use this trick to have only one index:

class Foo(sheraf.Model):
    table = "foos"
    bar = sheraf.ModelAttribute("Bar").index()

class Bar(sheraf.Model):
    table = "bars"

    @property 
    def foo(self):
        return Foo.search(bar=self)

    @foo.setter 
    def foo(self, value):
        value.bar = self

We should provide an attribute that does this automatically:

class Foo(sheraf.Model):
    table = "foos"
    bar = sheraf.ModelAttribute("Bar").index()

class Bar(sheraf.Model):
    table = "bars"
    foo = sheraf.InvertModelAttribute("Foo.bar")

We expect:

  • This attribute to only work if Foo.bar is indexed
  • Deletion the Bar object in Foo.bar when the Bar object is deleted.
  • This deletion trick should also work for LargeListAttribute(ModelAttribute(Bar))
  • Bar.foo should be settable
  • Bar.create should be able to take a foo argument
Edited by Éloi Rivard