我有两种模式:
class Actor(models.Model):
name = models.CharField(max_length=30, unique = True)
event = models.ManyToManyField(Event, blank=True, null=True)
class Event(models.Model):
name = models.CharField(max_length=30, unique = True)
long_description = models.TextField(blank=True, null=True)
在上一个问题:Django form linking 2 models by many to many field中,我创建了一个带保存功能的EventForm:
class EventForm(forms.ModelForm):
class Meta:
model = Event
def save(self, commit=True):
instance = forms.ModelForm.save(self)
instance.actors_set.clear()
for actor in self.cleaned_data['actors']:
instance.actors_set.add(actors)
return instance
这允许我从定义的m2m连接的另一侧添加m2m链接。
现在我想编辑条目。我一直在使用通用函数:
def generic_edit(request, modelname, object_id):
modelname = modelname.lower()
form_class = form_dict[modelname]
return update_object(request,
form_class = form_class,
object_id = object_id,
template_name = 'createdit.html'
)
但除了保存到此对象的多对多选项外,它会提取所有信息。
我想我需要做类似的事情:Editing both sides of M2M in Admin Page,但我还没弄明白。
如何使用通用update_object编辑多对多链接的另一面?