Welcome | Get started | Dive into Lino | Contribute | Reference

Why Lino doesn't use ManyToManyField

The ManyToManyField is an anti-feature.

Here is a classical use case:

class Topic(Model):
  name = CharField()

class Person(Model):
  topics = ManyToManyField(Topic)

We have persons and topics, and we want to say things like "Person X is interested in topics Y and Z" and "Person Foo is interested in topics Bar"

Django creates an automatic invisible table "PersonTopic" to store each "checked" topic on a person.


Usually it happens that you must extend an m2m field using the through keyword.

In Lino we prefer to say:

class Topic(dd.Model):
  name = CharField()

class Person(dd.Human):
  topics = ManyToManyField(Topic)

class Interest(dd.Model):
  topic = ForeignKey(Topic, related_name="interests_by_topic")
  person = ForeignKey(Person, related_name="interests_by_person")

class InterestsByPerson(dd.Table):
  model = "Interest"
  master_key = "person"

class InterestsByTopic(dd.Table):
  model = "Interest"
  master_key = "topic"

class Persons(dd.Table):
  detail_layout = """
  first_name last_name

class Product

specs = m2mfield(Spec)

class ProductSpec:

product = ForeignKey(Product) spec = ForeignKey(Spec)


model = "ProductSpec" master_key = 'product' insert_layout = """ spec spec__name """


model = "ProductSpec" master_key = 'spec'

ProductDetail = """ SpecsByProduct """