Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Django模型上测试外键对象_Django_Testing_Django Rest Framework - Fatal编程技术网

如何在Django模型上测试外键对象

如何在Django模型上测试外键对象,django,testing,django-rest-framework,Django,Testing,Django Rest Framework,我想在测试时传递模型(文章)上的外键对象(类别),我创建了category对象,然后将其变量传递给我的文章测试数据。我目前正在获取此错误TypeError:type'category'的对象不可JSON序列化。我的目标是能够创建一个category,然后在测试新文章的创建时将其传递到文章测试数据中。有什么想法吗正确地实施这一点 基本测试 class BaseTest: """ Class contains data to be used for testing """

我想在测试时传递模型(文章)上的外键对象(类别),我创建了category对象,然后将其变量传递给我的文章测试数据。我目前正在获取此错误
TypeError:type'category'的对象不可JSON序列化。
我的目标是能够创建一个category,然后在测试新文章的创建时将其传递到文章测试数据中。有什么想法吗正确地实施这一点

基本测试

class BaseTest:
    """
    Class contains data to be used for testing
    """

    def __init__(self):

        self.title = "How to tnnrain your flywwwwwwwwwwf"
        self.description = "Ever wondner how toddddd ddddwwwwd?"
        self.body = "You have to benlieve becausedddddddddcf"

        self.category = Category.objects.create(
            title='test category',
        )
        self.category.save()




       """ sample article data """
       self.create_article = {
            "article": {
                "title": self.title,
                "description": self.description,
                "body": self.body,
                "tags": ["reactjs"],
                "category": self.category


            }
        }
test_article.py

from rest_framework.test import APIClient, APITestCase
from rest_framework import status
from ..test_authentication.test_base import BaseTest
from authors.apps.articles.models import Article, Category
from authors.apps.authentication.models import User

class ArticlesTest(APITestCase,BaseTest):
    def setUp(self):
        BaseTest.__init__(self)
        self.client = APIClient()

    """ method for testing creation of a new article """
    def test_create_article(self):
        self.create_login_user()
        response = self.client.post('/api/articles/',self.create_article, 
        format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Models.py

class Category(models.Model):
      title = models.CharField(max_length=100)

      class Meta: 
            verbose_name_plural = "Categories"

      def __str__(self):
            return self.title

class Article(models.Model):

      title = models.CharField(max_length=255, null=False, blank=False)

      slug = models.SlugField(max_length=100, unique=True)

      description = models.TextField(null=False, blank=False)

      body = models.TextField(null=False, blank=False,)    

      category = models.ForeignKey(Category, 
                         on_delete=models.CASCADE)

由于使用JSON数据模拟HTTP POST,因此不应传递python
Category
对象,而应传递Category id:

class BaseTest:
"""
Class contains data to be used for testing
"""

    def __init__(self):

       self.title = "How to tnnrain your flywwwwwwwwwwf"
       self.description = "Ever wondner how toddddd ddddwwwwd?"
       self.body = "You have to benlieve becausedddddddddcf"

      self.category = Category.objects.create(
          title='test category',
         )
      # don't need to call save

    self.create_article = {
            "title": self.title,
            "description": self.description,
            "body": self.body,
            "tags": ["reactjs"],
            "category": self.category.id
      }

由于使用JSON数据模拟HTTP POST,因此不应传递python
Category
对象,而应传递Category id:

class BaseTest:
"""
Class contains data to be used for testing
"""

    def __init__(self):

       self.title = "How to tnnrain your flywwwwwwwwwwf"
       self.description = "Ever wondner how toddddd ddddwwwwd?"
       self.body = "You have to benlieve becausedddddddddcf"

      self.category = Category.objects.create(
          title='test category',
         )
      # don't need to call save

    self.create_article = {
            "title": self.title,
            "description": self.description,
            "body": self.body,
            "tags": ["reactjs"],
            "category": self.category.id
      }

您应该将self.category转换为json。
它不是json变量

您应该将self.category转换为json。
它不是json变量

请在序列化程序的create方法中执行,请在序列化程序的create方法中执行