2016-02-16 11:29:48 +03:00
|
|
|
import uuid
|
|
|
|
|
2022-06-08 15:41:26 +03:00
|
|
|
from django.contrib.auth.models import User
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.db import models
|
2022-11-22 09:01:01 +03:00
|
|
|
from django.db.models import QuerySet
|
|
|
|
from django.db.models.manager import BaseManager
|
Replace all usage ugettext functions with the non-u versions (#6634)
On Python 3, the ugettext functions are a simple aliases of their non-u
counterparts (the 'u' represents Python 2 unicode type). Starting with
Django 3.0, the u versions will be deprecated.
https://docs.djangoproject.com/en/dev/releases/3.0/#id2
> django.utils.translation.ugettext(), ugettext_lazy(), ugettext_noop(),
> ungettext(), and ungettext_lazy() are deprecated in favor of the
> functions that they’re aliases for:
> django.utils.translation.gettext(), gettext_lazy(), gettext_noop(),
> ngettext(), and ngettext_lazy().
2019-05-01 08:49:54 +03:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2012-10-05 01:07:24 +04:00
|
|
|
|
2012-09-28 17:28:50 +04:00
|
|
|
|
2012-10-04 12:19:10 +04:00
|
|
|
class RESTFrameworkModel(models.Model):
|
2012-10-03 13:51:38 +04:00
|
|
|
"""
|
2012-10-04 12:18:46 +04:00
|
|
|
Base for test models that sets app_label, so they play nicely.
|
2012-10-03 13:51:38 +04:00
|
|
|
"""
|
2014-11-29 21:45:26 +03:00
|
|
|
|
2012-10-03 13:51:38 +04:00
|
|
|
class Meta:
|
2012-10-27 21:44:23 +04:00
|
|
|
app_label = 'tests'
|
2012-10-04 12:18:46 +04:00
|
|
|
abstract = True
|
2012-10-03 13:51:38 +04:00
|
|
|
|
2012-09-28 17:28:50 +04:00
|
|
|
|
2012-10-04 12:19:10 +04:00
|
|
|
class BasicModel(RESTFrameworkModel):
|
2016-06-01 12:40:54 +03:00
|
|
|
text = models.CharField(
|
|
|
|
max_length=100,
|
|
|
|
verbose_name=_("Text comes here"),
|
|
|
|
help_text=_("Text description.")
|
|
|
|
)
|
2012-10-03 13:51:38 +04:00
|
|
|
|
|
|
|
|
2013-01-03 13:23:13 +04:00
|
|
|
# Models for relations tests
|
|
|
|
# ManyToMany
|
|
|
|
class ManyToManyTarget(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
|
|
|
|
class ManyToManySource(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
targets = models.ManyToManyField(ManyToManyTarget, related_name='sources')
|
|
|
|
|
|
|
|
|
2022-06-08 15:41:26 +03:00
|
|
|
class BasicModelWithUsers(RESTFrameworkModel):
|
|
|
|
users = models.ManyToManyField(User)
|
|
|
|
|
|
|
|
|
2013-01-03 13:23:13 +04:00
|
|
|
# ForeignKey
|
|
|
|
class ForeignKeyTarget(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2020-09-03 13:49:15 +03:00
|
|
|
def get_first_source(self):
|
|
|
|
"""Used for testing related field against a callable."""
|
|
|
|
return self.sources.all().order_by('pk')[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def first_source(self):
|
|
|
|
"""Used for testing related field against a property."""
|
|
|
|
return self.sources.all().order_by('pk')[0]
|
|
|
|
|
2013-01-03 13:23:13 +04:00
|
|
|
|
2016-02-16 11:29:48 +03:00
|
|
|
class UUIDForeignKeyTarget(RESTFrameworkModel):
|
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
|
2013-01-03 13:23:13 +04:00
|
|
|
class ForeignKeySource(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
2014-03-30 13:48:17 +04:00
|
|
|
target = models.ForeignKey(ForeignKeyTarget, related_name='sources',
|
2016-01-14 17:14:33 +03:00
|
|
|
help_text='Target', verbose_name='Target',
|
|
|
|
on_delete=models.CASCADE)
|
2013-01-03 13:23:13 +04:00
|
|
|
|
|
|
|
|
2019-01-08 16:49:47 +03:00
|
|
|
class ForeignKeySourceWithLimitedChoices(RESTFrameworkModel):
|
|
|
|
target = models.ForeignKey(ForeignKeyTarget, help_text='Target',
|
|
|
|
verbose_name='Target',
|
|
|
|
limit_choices_to={"name__startswith": "limited-"},
|
|
|
|
on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
|
2019-02-25 13:49:29 +03:00
|
|
|
class ForeignKeySourceWithQLimitedChoices(RESTFrameworkModel):
|
|
|
|
target = models.ForeignKey(ForeignKeyTarget, help_text='Target',
|
|
|
|
verbose_name='Target',
|
|
|
|
limit_choices_to=models.Q(name__startswith="limited-"),
|
|
|
|
on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
|
2013-01-03 13:23:13 +04:00
|
|
|
# Nullable ForeignKey
|
|
|
|
class NullableForeignKeySource(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
target = models.ForeignKey(ForeignKeyTarget, null=True, blank=True,
|
2014-03-30 13:48:17 +04:00
|
|
|
related_name='nullable_sources',
|
2016-01-14 17:14:33 +03:00
|
|
|
verbose_name='Optional target object',
|
|
|
|
on_delete=models.CASCADE)
|
2013-01-05 00:11:03 +04:00
|
|
|
|
2013-01-08 04:27:31 +04:00
|
|
|
|
2016-02-16 11:29:48 +03:00
|
|
|
class NullableUUIDForeignKeySource(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
target = models.ForeignKey(ForeignKeyTarget, null=True, blank=True,
|
|
|
|
related_name='nullable_sources',
|
|
|
|
verbose_name='Optional target object',
|
|
|
|
on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
|
2018-03-16 16:51:37 +03:00
|
|
|
class NestedForeignKeySource(RESTFrameworkModel):
|
|
|
|
"""
|
|
|
|
Used for testing FK chain. A -> B -> C.
|
|
|
|
"""
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
target = models.ForeignKey(NullableForeignKeySource, null=True, blank=True,
|
|
|
|
related_name='nested_sources',
|
|
|
|
verbose_name='Intermediate target object',
|
|
|
|
on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
|
2013-01-08 04:27:31 +04:00
|
|
|
# OneToOne
|
|
|
|
class OneToOneTarget(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
|
2013-01-05 00:11:03 +04:00
|
|
|
class NullableOneToOneSource(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
2016-06-01 12:40:54 +03:00
|
|
|
target = models.OneToOneField(
|
|
|
|
OneToOneTarget, null=True, blank=True,
|
|
|
|
related_name='nullable_source', on_delete=models.CASCADE)
|
2017-05-30 20:57:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
class OneToOnePKSource(RESTFrameworkModel):
|
|
|
|
""" Test model where the primary key is a OneToOneField with another model. """
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
target = models.OneToOneField(
|
|
|
|
OneToOneTarget, primary_key=True,
|
|
|
|
related_name='required_source', on_delete=models.CASCADE)
|
2022-11-22 09:01:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CustomManagerModel(RESTFrameworkModel):
|
|
|
|
class CustomManager:
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
cls = BaseManager.from_queryset(
|
|
|
|
QuerySet
|
|
|
|
)
|
|
|
|
return cls
|
|
|
|
|
|
|
|
objects = CustomManager()()
|
|
|
|
# `CustomManager()` will return a `BaseManager` class.
|
|
|
|
# We need to instantiation it, so we write `CustomManager()()` here.
|
|
|
|
|
|
|
|
text = models.CharField(
|
|
|
|
max_length=100,
|
|
|
|
verbose_name=_("Text comes here"),
|
|
|
|
help_text=_("Text description.")
|
|
|
|
)
|
|
|
|
|
|
|
|
o2o_target = models.ForeignKey(OneToOneTarget,
|
|
|
|
help_text='OneToOneTarget',
|
|
|
|
verbose_name='OneToOneTarget',
|
|
|
|
on_delete=models.CASCADE)
|
2024-10-19 21:41:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
class OwnershipTestModel(models.Model):
|
|
|
|
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='ownership_test_models')
|
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
def __str__(self):
|
2024-10-19 22:10:57 +03:00
|
|
|
return self.title
|