Add collection model

This commit is contained in:
Maximilian Friedersdorff 2022-07-04 21:47:59 +01:00
parent 8ba167823d
commit dc384710b2
3 changed files with 60 additions and 12 deletions

View file

@ -0,0 +1,33 @@
# Generated by Django 4.0.5 on 2022-07-04 20:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("posts", "0002_alter_post_body_alter_post_title"),
]
operations = [
migrations.CreateModel(
name="Collection",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=255)),
("description", models.TextField()),
(
"posts",
models.ManyToManyField(related_name="collections", to="posts.post"),
),
],
),
]

View file

@ -13,3 +13,13 @@ class Post(models.Model):
def __str__(self): def __str__(self):
return f"Post: {self.title} at {self.posted.strftime('%Y-%m-%d %H:%S')}" return f"Post: {self.title} at {self.posted.strftime('%Y-%m-%d %H:%S')}"
class Collection(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
posts = models.ManyToManyField("Post", related_name="collections")
def __str__(self):
return f"Collection: {self.title}"

View file

@ -3,24 +3,29 @@ import os
from django.test import TestCase from django.test import TestCase
from django.core.files import File from django.core.files import File
from .models import Post from .models import Post, Collection
# Create your tests here. # Create your tests here.
class TestPostModelTests(TestCase): class TestModelTests(TestCase):
def test_can_create_model(self): def setUp(self):
with open( with open(
os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"), os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"),
mode="rb", mode="rb",
) as f: ) as f:
try: self.post = Post.objects.create(
p = Post.objects.create( img=File(f, "somefile.png"),
img=File(f, "somefile.png"), title="Foobar",
title="Foobar", body="Some file",
body="Some file", )
)
self.assertIn("Foobar", str(p)) def tearDown(self):
finally: self.post.img.delete()
p.img.delete()
def test_post_has_sensible_str(self):
self.assertIn("Foobar", str(self.post))
def test_collection_has_sensible_str(self):
col = Collection.objects.create(title="A collection", description="foobar")
self.assertIn("A collection", str(col))