class Product(models.Model): class Meta: db_table = "store_product" ordering = [ 'id', ] id = fields.IntegerField(primary_key=True) name = fields.CharField(max_length=255) price = fields.CharField(max_length=16) images = fields.ManyToManyField( Image, through='ProductImage', ) categories = fields.ManyToManyField(Category, through='ProductCategory', related_name='products') brand = fields.ForeignKey(Brand, on_delete=fields.CASCADE, null=True, default=None, related_name='products') vendor = fields.ForeignKey(Vendor, on_delete=fields.CASCADE, null=True, default=None, related_name='products') def __str__(self): return self.name
class ProductImage(models.Model): class Meta: db_table = "store_productimage" unique_together = ('product', 'image') product = fields.ForeignKey(Product, on_delete=fields.CASCADE) image = fields.ForeignKey(Image, on_delete=fields.CASCADE)
class ProductCategory(models.Model): class Meta: db_table = "store_productcategory" unique_together = ('product', 'category') product = fields.ForeignKey(Product, on_delete=fields.CASCADE) category = fields.ForeignKey(Category, on_delete=fields.CASCADE)
class UniqueTogetherFieldsWithFK(Model): id = fields.IntegerField(primary_key=True) text = fields.CharField(max_length=64) tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKey( "models.Tournament") class Meta: unique_together = ("text", "tournament")
class Event(Model): id = fields.BigIntegerField(primary_key=True) name = fields.TextField() tournament: fields.ForeignKeyRelation["Tournament"] = fields.ForeignKey( "models.Tournament", related_name="events") reporter: fields.ForeignKeyNullableRelation[Reporter] = fields.ForeignKey( "models.Reporter", null=True) participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField( "models.Team", related_name="events", through="event_team", backward_key="idEvent") modified = fields.DateTimeField(auto_now=True) token = fields.TextField(default=generate_token) alias = fields.IntegerField(null=True) def __str__(self): return self.name
class UUIDFkRelatedSourceModel(Model): id = fields.UUIDField(primary_key=True, db_column="b") name = fields.CharField(max_length=50, null=True, db_column="c") model = fields.ForeignKey("models.UUIDPkSourceModel", related_name="children", db_column="d") class Meta: db_table = "ufrsm"
class UUIDFkRelatedNullModel(Model): id = fields.UUIDField(primary_key=True) name = fields.CharField(max_length=50, null=True) model: fields.ForeignKeyNullableRelation[UUIDPkModel] = fields.ForeignKey( "models.UUIDPkModel", related_name=False, null=True) parent: fields.OneToOneNullableRelation[ UUIDPkModel] = fields.OneToOneField("models.UUIDPkModel", related_name=False, null=True)
class Event(Model): id = fields.IntegerField(primary_key=True) name = fields.TextField() tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKey( "models.Tournament", related_name="events") participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField( "models.Team", related_name="events", through="event_team") def __str__(self): return self.name
class Employee(Model): name = fields.CharField(max_length=50) manager: fields.ForeignKeyNullableRelation["Employee"] = fields.ForeignKey( "models.Employee", related_name="team_members", null=True ) team_members: fields.ReverseRelation["Employee"] talks_to: fields.ManyToManyRelation["Employee"] = fields.ManyToManyField( "models.Employee", related_name="gets_talked_to" ) gets_talked_to: fields.ManyToManyRelation["Employee"] def __str__(self): return self.name async def full_hierarchy__async_for(self, level=0): """ Demonstrates ``async for` to fetch relations An async iterator will fetch the relationship on-demand. """ text = [ "{}{} (to: {}) (from: {})".format( level * " ", self, ", ".join(sorted([str(val) async for val in self.talks_to])), ", ".join(sorted([str(val) async for val in self.gets_talked_to])), ) ] async for member in self.team_members: text.append(await member.full_hierarchy__async_for(level + 1)) return "\n".join(text) async def full_hierarchy__fetch_related(self, level=0): """ Demonstrates ``await .fetch_related`` to fetch relations On prefetching the data, the relationship files will contain a regular list. This is how one would get relations working on sync serialisation/templating frameworks. """ await self.fetch_related("team_members", "talks_to", "gets_talked_to") text = [ "{}{} (to: {}) (from: {})".format( level * " ", self, ", ".join(sorted([str(val) for val in self.talks_to])), ", ".join(sorted([str(val) for val in self.gets_talked_to])), ) ] for member in self.team_members: text.append(await member.full_hierarchy__fetch_related(level + 1)) return "\n".join(text)
class User(Model): pk = 'id' ui = ['id', 'name', 'contact', 'profile'] id = fields.IntField(pk=True) name = fields.TextField() pw = fields.TextField() salt = fields.ForeignKey('models.Salt', related_name='salts') contact = fields.TextField() profile = fields.TextField() def __str__(self): return f"{self.name}"
class Category(models.Model): class Meta: db_table = "store_category" ordering = [ 'id', ] id = fields.IntegerField(primary_key=True) name = fields.CharField(max_length=255) image = fields.ForeignKey(Image, on_delete=fields.CASCADE, null=True) def __str__(self): return self.name
class Team(Model): name = fields.CharField(max_length=50, primary_key=True, description="The TEAM name (and PK)") key = fields.IntegerField() manager = fields.ForeignKey("models.Team", related_name="team_members", null=True) talks_to = fields.ManyToManyField("models.Team", related_name="gets_talked_to") class Meta: table_description = "The TEAMS!" indexes = [("manager", "key"), ["manager_id", "name"]]
class Event(Model): id = fields.IntegerField(primary_key=True, description="Event ID") name = fields.CharField(max_length=255, unique=True) tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKey( "models.Tournament", related_name="events", description="FK to tournament") participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField( "models.Team", related_name="events", through="event_team", description="How participants relate", ) modified = fields.DateTimeField(auto_now=True) prize = fields.DecimalField(max_digits=10, decimal_places=2, null=True) token = fields.CharField(max_length=100, description="Unique token", unique=True) class Meta: table_description = "This table contains a list of all the events"
class SourceFields(Model): id = fields.IntegerField(primary_key=True, db_column="sometable_id") chars = fields.CharField(max_length=255, db_column="some_chars_table", db_index=True) fk = fields.ForeignKey("models.SourceFields", related_name="team_members", null=True, db_column="fk_sometable") rel_to = fields.ManyToManyField( "models.SourceFields", related_name="rel_from", through="sometable_self", forward_key="sts_forward", backward_key="backward_sts", ) class Meta: db_table = "sometable" indexes = [["chars"]]
class Event(Model): id = fields.BigIntegerField(primary_key=True, description="Event ID") name = fields.TextField() tournament = fields.ForeignKey("models.Tournament", related_name="events", description="FK to tournament") participants = fields.ManyToManyField( "models.Team", related_name="events", through="teamevents", description="How participants relate", ) modified = fields.DateTimeField(auto_now=True) prize = fields.DecimalField(max_digits=10, decimal_places=2, null=True) token = fields.CharField(max_length=100, description="Unique token", unique=True) key = fields.CharField(max_length=100) class Meta: table_description = "This table contains a list of all the events" unique_together = [("name", "prize"), ["tournament", "key"]]
class SourceFields(Model): eyedee = fields.IntegerField(primary_key=True, db_column="sometable_id", description="Da PK") chars = fields.CharField( max_length=50, db_column="some_chars_table", db_index=True, description="Some chars" ) blip = fields.CharField(max_length=50, default="BLIP", db_column="da_blip") fk: fields.ForeignKeyNullableRelation["SourceFields"] = fields.ForeignKey( "models.SourceFields", related_name="fkrev", null=True, db_column="fk_sometable", description="Tree!", ) fkrev: fields.ReverseRelation["SourceFields"] o2o: fields.OneToOneNullableRelation["SourceFields"] = fields.OneToOneField( "models.SourceFields", related_name="o2o_rev", null=True, db_column="o2o_sometable", description="Line", ) o2o_rev: fields.Field rel_to: fields.ManyToManyRelation["SourceFields"] = fields.ManyToManyField( "models.SourceFields", related_name="rel_from", through="sometable_self", forward_key="sts_forward", backward_key="backward_sts", description="M2M to myself", ) rel_from: fields.ManyToManyRelation["SourceFields"] class Meta: db_table = "sometable" unique_together = [["chars", "blip"]] table_description = "Source mapped fields"
class StraightFields(Model): eyedee = fields.IntegerField(primary_key=True, description="Da PK") chars = fields.CharField(max_length=50, db_index=True, description="Some chars") blip = fields.CharField(max_length=50, default="BLIP") fk: fields.ForeignKeyNullableRelation["StraightFields"] = fields.ForeignKey( "models.StraightFields", related_name="fkrev", null=True, description="Tree!" ) fkrev: fields.ReverseRelation["StraightFields"] o2o: fields.OneToOneNullableRelation["StraightFields"] = fields.OneToOneField( "models.StraightFields", related_name="o2o_rev", null=True, description="Line" ) o2o_rev: fields.Field rel_to: fields.ManyToManyRelation["StraightFields"] = fields.ManyToManyField( "models.StraightFields", related_name="rel_from", description="M2M to myself" ) rel_from: fields.ManyToManyRelation["StraightFields"] class Meta: unique_together = [["chars", "blip"]] table_description = "Straight auto-mapped fields"
class CharFkRelatedModel(Model): model = fields.ForeignKey(CharPkModel, related_name="children")
class DoubleFK(Model): name = fields.CharField(max_length=50) left = fields.ForeignKey("models.DoubleFK", null=True, related_name="left_rel") right = fields.ForeignKey("DoubleFK", null=True, related_name="right_rel")
class UUIDFkRelatedModel(Model): id = fields.UUIDField(primary_key=True) name = fields.CharField(max_length=50, null=True) model: fields.ForeignKeyRelation[UUIDPkModel] = fields.ForeignKey( "models.UUIDPkModel", related_name="children")
class TeamEvent(Model): te_event = fields.ForeignKey("models.Event") te_host = fields.ForeignKey("models.Team", related_name="hosting_events") te_guest = fields.ForeignKey("models.Team", related_name="asguest_events")
class One(Model): tournament = fields.ForeignKey("models.Two", on_delete=fields.SET_NULL)
class Choice(models.Model): question = fields.ForeignKey('models.Question', ) choice_text = fields.CharField(max_length=200) votes = fields.IntegerField(default=0)
class TeamEvent(Model): # te_event = fields.ForeignKey("models.Event") te_team = fields.ForeignKey("models.Team")
class Three(Model): tournament = fields.ForeignKey("models.One", related_name="events")
class Party(Model): tournament = fields.ForeignKey("models.Tournament", related_name="events")
class One(Model): tournament = fields.ForeignKey("moo")
class MinRelation(Model): id = fields.IntegerField(primary_key=True) tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKey( "models.Tournament") participants: fields.ManyToManyRelation[Team] = fields.ManyToManyField( "models.Team")
class One(Model): tournament = fields.ForeignKey("models.Two", related_name="events")
class Event(Model): tournament = fields.ForeignKey("app.Tournament", related_name="events")