我有MultipleChoiceField表格。它显示了来自元组CHOICES的数据。用户选择复选框,然后我使用所选数据在视图内创建新对象(在我的情况下是要求)。当我尝试在我的视图中使用 django-reversion 时,它会引发错误。你有什么想法为什么reversion.set_user(request.user)
不reversion.set_comment('CREATE')
工作?
模型.py:
@reversion.register()
class Requirement(models.Model):
code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
symbol = models.CharField(_('Symbol'), max_length=250)
name = models.CharField(_('Name'), max_length=250)
表格.py:
CHOICES = (
('A', 'Name A'),
('B', 'Name B'),
('C', 'Name C'),
)
class RequirementAddForm(forms.ModelForm):
symbol = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=CHOICES,)
class Meta:
model = Requirement
fields = ('symbol',)
视图.py:
@reversion.create_revision()
def _requirement_add(request):
if request.method == 'POST':
form = RequirementAddForm(request.POST)
if form.is_valid():
group_requirement_list = dict(CHOICES) # {'C': 'Name C', 'A': 'Name A', 'B': 'Name B'}
symbols = form.cleaned_data.get('symbol') # Selected values: ['A', 'B', 'C']
group_requirement = list_group_requirement_form.save(commit=False)
for symbol in symbols:
group_requirement.project = project
group_requirement.symbol = symbol
group_requirement.name = group_requirement_list[symbol]
group_requirement.pk = None
group_requirement.save()
reversion.set_user(request.user) # ???
reversion.set_comment('CREATE') # ???
错误:
Traceback (most recent call last):
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 296, in do_revision_context
return func(*args, **kwargs)
File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 603, in requirement_add
requirement.save()
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\base.py", line 833, in save_base
update_fields=update_fields, raw=raw, using=using)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\dispatch\dispatcher.py", line 191, in send
response = receiver(signal=self, sender=sender, **named)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 302, in _post_save_receiver
add_to_revision(instance, model_db=using)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 212, in add_to_revision
_add_to_revision(obj, db, model_db, True)
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 193, in _add_to_revision
object_repr=force_text(obj),
File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\encoding.py", line 76, in force_text
s = six.text_type(s)
TypeError: __str__ returned non-string (type __proxy__)
[26/Apr/2017 13:17:43] "POST /ru/account/dashboard/projects/cdecbd12-80d3-4ecd-9c08-8edef5d7f850/requirement_add/ HTTP/1.1" 500 18847