2015-09-26 17:12:30 +00:00
|
|
|
"""Models for test_natural.py"""
|
2018-11-22 21:11:51 +00:00
|
|
|
import uuid
|
|
|
|
|
2015-09-26 17:12:30 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
class NaturalKeyAnchorManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, data):
|
|
|
|
return self.get(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
class NaturalKeyAnchor(models.Model):
|
|
|
|
data = models.CharField(max_length=100, unique=True)
|
|
|
|
title = models.CharField(max_length=100, null=True)
|
|
|
|
|
2018-12-28 00:34:14 +00:00
|
|
|
objects = NaturalKeyAnchorManager()
|
|
|
|
|
2015-09-26 17:12:30 +00:00
|
|
|
def natural_key(self):
|
|
|
|
return (self.data,)
|
|
|
|
|
|
|
|
|
|
|
|
class FKDataNaturalKey(models.Model):
|
|
|
|
data = models.ForeignKey(NaturalKeyAnchor, models.SET_NULL, null=True)
|
2018-07-13 21:54:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NaturalKeyThing(models.Model):
|
2020-04-07 08:54:19 +00:00
|
|
|
key = models.CharField(max_length=100, unique=True)
|
2018-07-13 21:54:47 +00:00
|
|
|
other_thing = models.ForeignKey('NaturalKeyThing', on_delete=models.CASCADE, null=True)
|
|
|
|
other_things = models.ManyToManyField('NaturalKeyThing', related_name='thing_m2m_set')
|
|
|
|
|
|
|
|
class Manager(models.Manager):
|
|
|
|
def get_by_natural_key(self, key):
|
|
|
|
return self.get(key=key)
|
|
|
|
|
|
|
|
objects = Manager()
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.key,)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.key
|
2018-11-22 21:11:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NaturalPKWithDefault(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
name = models.CharField(max_length=100, unique=True)
|
|
|
|
|
|
|
|
class Manager(models.Manager):
|
|
|
|
def get_by_natural_key(self, name):
|
|
|
|
return self.get(name=name)
|
|
|
|
|
|
|
|
objects = Manager()
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.name,)
|