我有 MultipleChoiceField 的表单。它显示了来自元组 CHOICES 的数据。用户选择复选框,然后我使用所选数据在视图内创建新对象(在我的情况下需求)。当我尝试在我的视图中使用django-reversion时会引发Error。您有什么想法"
和reversion.set_user(request.user)
不起作用吗?
models.py:
reversion.set_comment('CREATE')
forms.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)
view.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',)
错误:
@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') # ???
答案 0 :(得分:0)
最后,我发现了问题。在我的模态中,我使用:
def __str__(self):
return self.name
删除django-reversion后工作正确!