Skip to content

Instantly share code, notes, and snippets.

@lu911
Created January 30, 2022 16:58
Show Gist options
  • Save lu911/6cc9506bafc4a33dd17206a414a39234 to your computer and use it in GitHub Desktop.
Save lu911/6cc9506bafc4a33dd17206a414a39234 to your computer and use it in GitHub Desktop.
Django M2M
# -*- coding: utf-8 -*-
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
group = models.ForeignKey("Group", on_delete=models.CASCADE)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment