Skip to content

Commit 59fcbc6

Browse files
committed
Add migration for generic relations Tag model.
1 parent 481ae69 commit 59fcbc6

File tree

6 files changed

+84
-43
lines changed

6 files changed

+84
-43
lines changed

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def pytest_configure(config):
5757
'rest_framework',
5858
'rest_framework.authtoken',
5959
'tests.authentication',
60+
'tests.generic_relations',
6061
'tests.importable',
6162
'tests',
6263
),

tests/generic_relations/__init__.py

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from django.db import migrations, models
2+
3+
4+
class Migration(migrations.Migration):
5+
6+
initial = True
7+
8+
dependencies = [
9+
('contenttypes', '0002_remove_content_type_name'),
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name='Bookmark',
15+
fields=[
16+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
17+
('url', models.URLField()),
18+
],
19+
),
20+
migrations.CreateModel(
21+
name='Note',
22+
fields=[
23+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
24+
('text', models.TextField()),
25+
],
26+
),
27+
migrations.CreateModel(
28+
name='Tag',
29+
fields=[
30+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
31+
('tag', models.SlugField()),
32+
('object_id', models.PositiveIntegerField()),
33+
('content_type', models.ForeignKey(on_delete=models.CASCADE, to='contenttypes.ContentType')),
34+
],
35+
),
36+
]

tests/generic_relations/migrations/__init__.py

Whitespace-only changes.

tests/generic_relations/models.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import unicode_literals
2+
3+
from django.contrib.contenttypes.fields import (
4+
GenericForeignKey, GenericRelation
5+
)
6+
from django.contrib.contenttypes.models import ContentType
7+
from django.db import models
8+
from django.utils.encoding import python_2_unicode_compatible
9+
10+
11+
@python_2_unicode_compatible
12+
class Tag(models.Model):
13+
"""
14+
Tags have a descriptive slug, and are attached to an arbitrary object.
15+
"""
16+
tag = models.SlugField()
17+
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
18+
object_id = models.PositiveIntegerField()
19+
tagged_item = GenericForeignKey('content_type', 'object_id')
20+
21+
def __str__(self):
22+
return self.tag
23+
24+
25+
@python_2_unicode_compatible
26+
class Bookmark(models.Model):
27+
"""
28+
A URL bookmark that may have multiple tags attached.
29+
"""
30+
url = models.URLField()
31+
tags = GenericRelation(Tag)
32+
33+
def __str__(self):
34+
return 'Bookmark: %s' % self.url
35+
36+
37+
@python_2_unicode_compatible
38+
class Note(models.Model):
39+
"""
40+
A textual note that may have multiple tags attached.
41+
"""
42+
text = models.TextField()
43+
tags = GenericRelation(Tag)
44+
45+
def __str__(self):
46+
return 'Note: %s' % self.text

tests/test_relations_generic.py renamed to tests/generic_relations/test_generic_relations.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,10 @@
11
from __future__ import unicode_literals
22

3-
from django.contrib.contenttypes.fields import (
4-
GenericForeignKey, GenericRelation
5-
)
6-
from django.contrib.contenttypes.models import ContentType
7-
from django.db import models
83
from django.test import TestCase
9-
from django.utils.encoding import python_2_unicode_compatible
104

115
from rest_framework import serializers
126

13-
14-
@python_2_unicode_compatible
15-
class Tag(models.Model):
16-
"""
17-
Tags have a descriptive slug, and are attached to an arbitrary object.
18-
"""
19-
tag = models.SlugField()
20-
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
21-
object_id = models.PositiveIntegerField()
22-
tagged_item = GenericForeignKey('content_type', 'object_id')
23-
24-
def __str__(self):
25-
return self.tag
26-
27-
28-
@python_2_unicode_compatible
29-
class Bookmark(models.Model):
30-
"""
31-
A URL bookmark that may have multiple tags attached.
32-
"""
33-
url = models.URLField()
34-
tags = GenericRelation(Tag)
35-
36-
def __str__(self):
37-
return 'Bookmark: %s' % self.url
38-
39-
40-
@python_2_unicode_compatible
41-
class Note(models.Model):
42-
"""
43-
A textual note that may have multiple tags attached.
44-
"""
45-
text = models.TextField()
46-
tags = GenericRelation(Tag)
47-
48-
def __str__(self):
49-
return 'Note: %s' % self.text
7+
from .models import Bookmark, Note, Tag
508

519

5210
class TestGenericRelations(TestCase):

0 commit comments

Comments
 (0)