I have a modelform with a model which include a models.many_to_many field.
By default the modelform represents those m2m entries with a
multiplechoice field
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
example:
tag_set = forms.ModelMultipleChoiceField(queryset=Tag.objects.all())
1/ when I initialize my model form (form(instance=defaultinstance)), I
would like the m2m entries to
appear as a form.TextInput with default value of defaultinstance.tag_set.all()
2/ To save the tags, I have
class AssignmentEditForm(ModelForm):
[...]
def save_m2m(self):
tagNames = form.cleaned_data['tag_set'].split()
for tagName in tagNames:
tag, dummy = Tag.objects.get_or_create(name=tagName)
self.__assignment.tag_set.add(tag)
class Meta:
MODEL = Assignment
widgets = {
'tagline' : forms.TextInput(attrs={'size':70}),
'description' : forms.Textarea(attrs={'cols':80, 'rows':5}),
'tag_set': forms.TextInput(attrs={'size':70}),
}
How can I get the NEW instance in the save_m2m(self) for
self.__assignment.tag_set.add(tag) ?
(I had to overwrite save method as well with following
def save(self,commit=True):
''' Overwrite the default model save '''
self.__assignment = super(AssignmentEditForm,self).save(commit=False)
self.__assignment.save()
self.save_m2m()
Is there a better way to do that?
--
Emmanuel Mayssat
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment