Я пытаюсь написать код для редактирования записей и его модульного теста. вот мой код: test_view.py
def test_edit_address(self):
address1 = Address.objects.create(first_name='Edith', last_name='Star', address='Some City', phone_number='123455')
response = self.client.post(
reverse('edit', kwargs={'address_id': address1.id}),
{'first_name': 'Patrick', 'last_name': 'Stars', 'address': 'Other City', 'phone_number': '0123455'})
self.assertEqual(response.status_code, 302)
address1.refresh_from_db()
self.assertEqual(address1.first_name, 'Patrick')
views.py
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import redirect, render
from addressbooksapp.models import Address
from django.contrib import messages
def edit(request, address_id):
if request.method == 'POST':
addresses = Address.objects.get(pk=address_id)
addresses.first_name = request.POST.get('firstname_text')
addresses.last_name = request.POST.get('lastname_text')
addresses.address = request.POST.get('address_text')
addresses.phone_number = request.POST.get('phone_text')
addresses.save()
messages.success(request, ('Record Has Been Edited!'))
return redirect('home')
else:
addresses = Address.objects.get(pk=address_id)
return render(request, 'edit.html', {'addresses': addresses})
models.py
from django.db import models
class Address(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
address = models.TextField()
phone_number = models.CharField(max_length=200)
urls.py
from django.urls import path
from addressbooksapp import views
urlpatterns = [
path('', views.home_page, name='home'),
path('delete/<address_id>',views.delete, name='delete'),
path('edit/<address_id>',views.edit, name='edit'),
]
Но при запуске теста я получил ошибку "django .db.utils.IntegrityError: Сбой ограничения NOT NULL: addressbooksapp_address.first_name"