2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2016-02-16 11:29:48 +03:00
|
|
|
import uuid
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.db import models
|
2012-12-03 22:07:07 +04:00
|
|
|
from django.utils.translation import ugettext_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')
|
|
|
|
|
|
|
|
|
|
|
|
# ForeignKey
|
|
|
|
class ForeignKeyTarget(RESTFrameworkModel):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
|
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)
|