我的问题是我想使用ModelForm管理对话属性。现在我需要实现添加对话参与者和更改对话名称。我尝试使用下面的代码执行此操作,但是当我添加参与者时,对话框名称变为空字符串。更改名称根本不起作用。
我的问题是为什么change_name方法没有效果,为什么在添加新参与者后 - 对话名称变为空字符串。
此对话模型包含用于更改名称 - (def change_name)和添加参与者(def add_participants)的方法:
class Dialogue(models.Model):
name = models.CharField(max_length=30, blank=True)
is_conference = models.BooleanField(default=False)
notifications_on = models.BooleanField(default=True)
creator = models.ForeignKey(
Person,
related_name='+',
null=True,
)
participants = models.ManyToManyField(
Person,
related_name='dialogues',
)
def __str__(self):
return self.name or str(self.pk)
def add_participants(self, *args):
new_participants = args
for new_participant in new_participants:
self.participants.add(new_participant)
return self
def remove_participants(self, *args):
participants = args
for participant in participants:
self.participants.remove(participant)
return self
def change_name(self, new_name):
self.name = new_name
return self
在这个ModelForm中,我创建了具有is_conference属性的Dialogue,该属性总是等于true。我分了两个机会:当我创建会议和管理现有会议时。所以我第二部分有问题。我试图删除我调用函数change_name的部分,但是在添加新参与者之后,对话名称仍然变空。
class CreateConferenceForm(forms.ModelForm):
class Meta:
model = Dialogue
fields = ['name', 'participants']
def __init__(self, person, *args, **kwargs):
self.person = person
super().__init__(*args, **kwargs)
def save(self):
cleaned_data = super(CreateConferenceForm, self).clean()
if self.instance not in self.person.dialogues.all():
dialogue = Dialogue(creator=self.person, name=cleaned_data.get('name'), is_conference=True)
dialogue.save()
dialogue.add_participants(self.person, *cleaned_data.get('participants'))
else:
dialogue = self.instance
if cleaned_data.get('name'):
dialogue.change_name(cleaned_data.get('name'))
dialogue.save()
if len(cleaned_data.get('participants')):
dialogue.add_participants(*cleaned_data.get('participants'))
此视图使用ModelForm呈现页面:
def dialogue(request):
person = Person.objects.get(id=request.user.id)
sel = request.GET.get('sel')
act = request.GET.get('act')
try:
managable_dialogue = person.dialogues.filter(is_conference=True)[int(sel[1:])-1]
form = CreateConferenceForm(instance=managable_dialogue, person=person)
new_available_participants = person.friends
for person in managable_dialogue.participants.all():
new_available_participants = new_available_participants.exclude(id=person.id)
form.fields['participants'].queryset = new_available_participants.all()
return render(request, 'dialogues/dialogue_creation.html', {
'form': form,
'act': act,
'sel': sel,
})
except ObjectDoesNotExist:
return HttpResponseRedirect('/im')
第二个视图从前一个视图获取POST数据并保存更改。我在这里测试了is_valid的结果,如果我只改变对话名称,则它是假的,但如果我只添加参与者,则它是真的。这两项更改我还没有测试,因为我只需要单独更改:
def edit(request):
person = Person.objects.get(id=request.user.id)
sel = request.POST.get('sel')
data = request.POST
try:
managable_dialogue = person.dialogues.filter(is_conference=True)[int(sel[1:]) - 1]
form_data = CreateConferenceForm(instance=managable_dialogue, person=person, data=data)
if form_data.is_valid():
form_data.save()
except:
pass
finally:
return HttpResponseRedirect('/im?sel='+sel)
因此,添加新参与者会将对话名称覆盖为空字符串,但会添加新参与者,并且更改对话名称根本无效。