models for testnatural py import uuid from django db import models class naturalkeyanchormanagermodels manager def getbynaturalkeyself data return self getdatadata class naturalkeyanchormodels model data models charfieldmaxlength100 uniquetrue title models charfieldmaxlength100 nulltrue objects naturalkeyanchormanager def naturalkeyself return self data class fkdatanaturalkeymodels model data models foreignkeynaturalkeyanchor models setnull nulltrue class naturalkeythingmodels model key models charfieldmaxlength100 uniquetrue otherthing models foreignkey naturalkeything ondeletemodels cascade nulltrue otherthings models manytomanyfield naturalkeything relatednamethingm2mset class managermodels manager def getbynaturalkeyself key return self getkeykey objects manager def naturalkeyself return self key def strself return self key class naturalpkwithdefaultmodels model id models uuidfieldprimarykeytrue defaultuuid uuid4 editablefalse name models charfieldmaxlength100 uniquetrue class managermodels manager def getbynaturalkeyself name return self getnamename objects manager def naturalkeyself return self name class fkaspknonaturalkeymanagermodels manager def getbynaturalkeyself args kwargs return super getbynaturalkeyargs kwargs class fkaspknonaturalkeymodels model pkfk models foreignkey naturalkeyanchor ondeletemodels cascade primarykeytrue objects fkaspknonaturalkeymanager def naturalkeyself raise notimplementederrorthis method was not expected to be called
import uuid

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)

    objects = NaturalKeyAnchorManager()

    def natural_key(self):
        return (self.data,)


class FKDataNaturalKey(models.Model):
    data = models.ForeignKey(NaturalKeyAnchor, models.SET_NULL, null=True)


class NaturalKeyThing(models.Model):
    key = models.CharField(max_length=100, unique=True)
    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


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,)


class FKAsPKNoNaturalKeyManager(models.Manager):
    def get_by_natural_key(self, *args, **kwargs):
        return super().get_by_natural_key(*args, **kwargs)


class FKAsPKNoNaturalKey(models.Model):
    pk_fk = models.ForeignKey(
        NaturalKeyAnchor, on_delete=models.CASCADE, primary_key=True
    )

    objects = FKAsPKNoNaturalKeyManager()

    def natural_key(self):
        raise NotImplementedError("This method was not expected to be called.")