Django使用Foreign Key和UpdateView测试简单模型

sirbozc5  于 2023-06-07  发布在  Go
关注(0)|答案(1)|浏览(192)

两个非常简单的模型

from django.db import models

class Location(models.Model):
    name = models.CharField(max_length=255)
    location_number = models.CharField(max_length=30, unique=True)

    def __str__(self):
        return self.name

class Project(models.Model):
    location = models.ForeignKey(Location, on_delete=models.CASCADE, to_field='location_number', db_column='location_number', related_name='projects', verbose_name='Standort')
    name = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)

    def __str__(self):
        return self.name

使用UpdateView

from .models import Project
from django.views.generic import UpdateView

class ProjectUpdateView(UpdateView):
    model = Project
    fields = "__all__"
    slug_url_kwarg = 'project_slug'
    slug_field = 'slug'

应该用这个简单的测试来测试:

from django.test import TestCase
from django.urls import reverse
from .models import Project, Location
from urllib.parse import urlencode
from django.utils.http import urlencode

class ProjectUpdateTest(TestCase):
    def setUp(self):
        self.location = Location.objects.create(
            name='Test Location',
        )
        
        self.project = Project.objects.create(
            location=self.location,
            slug="test",
            name='Test Project',
        )

    def test_project_update_view(self):
        # Prepare the data for the form
        form_data = {
            'name': 'Updated Project',
            'location': self.location.pk,
            'slug': 'test',
        }

        # Build the URL for the project detail view
        url = reverse('update_project', kwargs={
            'project_slug': self.project.slug
        })

        # Encode the form data as form-encoded data
        encoded_form_data = urlencode(form_data, doseq=True)

        # Send a POST request to the project update view with the form-encoded data
        response = self.client.post(url, data=encoded_form_data, content_type='application/x-www-form-urlencoded')

        # Print form errors, if any
        form = response.context['form']
        print(form.errors)

        self.project.refresh_from_db()
        self.assertEqual(self.project.name, 'Updated Project')

但它失败了

  • 位置
  • 选择有效选项。该选项不是可用选项之一。F ===============================================================失败:test_project_update_view(playground.tests.ProjectUpdateTest.test_project_update_view)-----------------------------------------------------------------------------追溯(最近一次调用):File“/code/playground/tests.py“,line 43,in test_project_update_view self.assertEqual(self.project.name,'Updated Project')AssertionError:“测试项目”!= '更新项目' -测试项目+更新项目

无论我尝试什么

  • 'location':self.location.pk
  • 'location':self.location,
  • 'location':self.location.id,

表单不接受该位置作为有效选择
选择有效选项。该选项不是可用选项之一。
有人能帮我处理一下吗?- 谢谢你!

zlhcx6iw

zlhcx6iw1#

你的to_fieldto_field='location_number',所以你传递location的名字:

form_data = {
    'name': 'Updated Project',
    'location': self.location.location_number,
    'slug': 'test',
}

因此,在这里,这可能是空字符串。

相关问题