2017-04-26 16 views
0

私はを持っています。複数選択肢です。それはタプルの選択肢からデータを表示するです。ユーザーはチェックボックスを選択し、その選択したデータを使用してビュー内で新しいオブジェクトを作成します。私のビューでdjango-reversionを使用しようとすると、Errorが発生します。 reversion.set_user(request.user)reversion.set_comment('CREATE')がうまくいかない理由がありますか?Django-ReversionはMultipleChoiceFieldで動作しませんでしたか?

models.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) 

forms.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',) 

view.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 

答えて

0

最後に、問題が見つかりました。私のモーダルで私は使用します:

def __str__(self): 
    return self.name 

削除した後にdjango-reversionが正しく動作します!

+1

完全に削除したくない場合は、 'return str(self.name)'に変更してみてください。 – Alasdair

関連する問題