문제

I have two models:

 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)

In a previous question: Django form linking 2 models by many to many field, I created an EventForm with a save function:

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

This allowed me to add m2m links from the other side of the defined m2m connection.

Now I want to edit the entry. I've been using a generic function:

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'
       )

but this pulls in all the info except the many-to-many selections saved to this object.

I think I need to do something similar to this: Editing both sides of M2M in Admin Page, but I haven't figured it out.

How do I use the generic update_object to edit the other side of many-to-many link?

도움이 되었습니까?

해결책

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top