Test.py
这些文件用于测试memepost应用程序中的所有内容。
from django.test import TestCase
from memepost.models import memepost, memepostForm
from datetime import date
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class memepostTestCase(TestCase):
def setUp(self):
memepost.objects.create(title="foo",
model_pic="/site_media/media/pic_folder/casual_headshot.jpg",
date=date(2017, 05, 14))
memepost.objects.create(title="bar",
date=date(2017,05, 1))
def test_memepost_enters_database(self):
foo = memepost.objects.get("foo")
bar = memepost.objects.get("bar")
self.assertEqual(foo.date, "6/15/17")
logger.debug("foo date is equal to %s", foo.date)
self.assertEqual(bar.model_pic, "no-img.jpg")
logger.debug("bar is equal to %s", bar.model_pic)
models.py
此文件用作在我的数据库中创建模因的方案。
from __future__ import unicode_literals
from django.forms import ModelForm
from django.db import models
# Create your models here.
class memepost(models.Model):
title = models.CharField(max_length=140)
model_pic = models.ImageField(upload_to='pic_folder/', default='pic_folder/no-img.jpg')
date = models.DateTimeField()
def __unicode__(self):
return self.title
class memepostForm(ModelForm):
class Meta:
model = memepost
fields = ["title", "model_pic", "date"]
错误
好的,所以我收到了这个警告,但我认为问题与标题参数有关,因为我通过标题调用了get函数。我希望我很清楚。
WARNING:py.warnings:/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1393:
RuntimeWarning: DateTimeField memepost.date received a naive datetime (2017-05-01 00:00:00) while time zone support is active.
RuntimeWarning)
======================================================================
ERROR: test_memepost_enters_database (memepost.tests.memepostTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ubuntu/workspace/mysite/memepost/tests.py", line 20, in test_memepost_enters_database
foo = memepost.objects.get("foo")
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 378, in get
clone = self.filter(*args, **kwargs)
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 790, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1243, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1146, in build_filter
arg, value = filter_expr
ValueError: too many values to unpack
----------------------------------------------------------------------
Ran 1 test in 0.007s
答案 0 :(得分:1)
您没有为get
经理方法
def test_memepost_enters_database(self):
foo = memepost.objects.get(title="foo")
bar = memepost.objects.get(title="bar")
...