forked from sarvex/pants-example-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
30 lines (21 loc) · 915 Bytes
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright 2021 Pants project contributors.
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from django.db import models
from helloworld.greet.models import Greeting
class TranslationManager(models.Manager):
def get_queryset(self):
qs = super().get_queryset()
return qs.select_related("greeting")
class Translation(models.Model):
class Meta:
constraints = [
models.UniqueConstraint(fields=["greeting", "lang"], name="greeting_lang")
]
objects = TranslationManager()
# NB: This Translation model share a database with the Greeting model, so this
# relation is allowed by our database router.
greeting = models.ForeignKey(Greeting, on_delete=models.CASCADE)
lang = models.CharField(max_length=2)
translation = models.CharField(max_length=20)
def __str__(self):
return f"{self.greeting} in {self.lang}"