0
django docsによれば、モデルフォームフィールドはattrs
を受け入れます。私はattrs
を適用しようとすると、私は私が作るしようとしてるのフォームは非常に単純ですModelFormフィールドattrs
TypeError: init() got an unexpected keyword argument 'attrs'
を取得し、私はちょうどそれにスタイルを適用したいです。私は間違って何をしていますか?
forms.py
from django import forms
from .models import ServiceReportModel
class ServiceReportCreateForm(forms.ModelForm):
class Meta:
model = ServiceReportModel
fields = [
'request_number',
'request_reason',
'actions_taken',
]
class ServiceReportUpdateForm(forms.ModelForm):
class Meta:
model = ServiceReportModel
fields = [
'report_number',
'request_number',
'request_reason',
'actions_taken',
]
widgets = {
'report_number': forms.CharField(attrs={'class': 'form-control'}),
'request_number': forms.CharField(attrs={'class': 'form-control'}),
'request_reason': forms.CharField(attrs={'class': 'form-control'}),
'actions_taken': forms.Textarea(attrs={'class': 'form-control'}),
}
views.py
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .forms import ServiceReportCreateForm, ServiceReportUpdateForm
from .models import ServiceReportModel
class ReportCreateView(CreateView):
form_class = ServiceReportCreateForm
model = ServiceReportModel
class ReportCreateView(UpdateView):
form_class = ServiceReportUpdateForm
model = ServiceReportModel
class ReportDeleteView(DeleteView):
model = ServiceReportModel
success_url = reverse_lazy('reports-list')
models.py
import uuid
from django.urls import reverse
from django.db import models
from django.forms import ModelForm
from main import models as main_models
from customers import models as customers_models
class ServiceReportModel(models.Model):
report_number = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
request_number = models.ForeignKey(ServiceRequestModel,
on_delete=models.PROTECT,
null=True,
related_name='s_report_number'
)
reported_by = models.ForeignKey(main_models.MyUser, editable=False, related_name='reports')
reported_date = models.DateTimeField(auto_now_add=True)
updated_by = models.ForeignKey(main_models.MyUser, editable=True, blank=True, null=True, related_name='+')
updated_date = models.DateTimeField(auto_now=True)
request_reason = models.CharField(max_length=255, null=True)
actions_taken = models.TextField()
def get_absolute_url(self):
return reverse('service-report', kwargs={'pk': self.pk})
はい、そうです。ありがとうございました。私はすでに投稿した後に気づいたが、良い情報はありません。 – Jaberwocky