Hi Camilo, I really appreciate your response. I had another go at this last night, particularly trying to use the add(component()) snippet.
To respond to your notes: 1. I simplified and used a find-replace to replace the model names which is likely why it dosn't compile, sorry about that. Mine compiles and i'm happy to PM you the code if you'd like. 2. I have very limited programming experience at this point and my project is the cumulation of experimentation, examples and documentation i've read... I believe your saying I don't need to use the "instance"s? 3. Good point, thank you. 4. I've added the lines which try to ensure the m2m relationships are added, such as new_item.category.add(new_item) new_item.components.add(new_item) during my attempts to get the form to save the many to many data. What i'm trying to do is create a basic form which allows a user to fill in the name, desc, img, category and component fields When they save this form, i'd like the "item" to be saved (which is happening) as well as the user, category and component data to be associated to the item. Currently only the user is being associated to the item when I complete the form and save it. Once I can do that, i'll move on to using widgets to make the category field a drop down and the component field/s tick boxes and continue adding form validation etc but i've been stuck on this saving issue for some time. Thanks again for your time and help. On Monday, 7 April 2014 10:58:21 UTC+12, Camilo Torres wrote: > > On Saturday, April 5, 2014 10:12:36 PM UTC-4:30, Jason S wrote: >> >> I've seen quite a few examples showing various ways to create a form >> using manytomany relationships and save the data, but i'm still missing a >> key piece of the puzzle. >> I've created a form and it "works", meaning the form creates a new Item >> object and associates the user. >> However i'm not sure how to go about saving the item category and item >> components selected in the form. >> >> I've tried quite a few things over the last week and i know its quite >> easy, but just haven't quite gotten there yet and would appreciate some >> guidance. >> >> FORM: >> class NewItemForm(ModelForm): >> >> class Meta: >> model = Item >> fields = ('name', 'desc', 'img','category','components') >> >> def save(self, user, component commit = True): >> """Append the component list to the item""" >> new_item = super(NewItemForm, self).save(commit = Fals >> >> if commit: >> new_item.save() >> # new_item.save_m2m() >> new_item.user.add(user) >> if component != "none": >> new_item.category.add(new_item) >> new_item.components.add(new_item) >> >> return new_item >> >> VIEW: >> def create_item(request): >> >> if request.method == "POST": >> form = NewItemForm(request.POST, instance=item()) >> if form.is_valid(): >> form.save(request.user, "none") >> return HttpResponseRedirect(reverse('nav.views.dashboard')) >> else: >> form = CategoryForm(instance=item()) >> return render_to_response('objects/create_item.html', >> {'item_Form': form}, context_instance=RequestContext(request)) >> >> MODEL: >> class Item(models.Model): >> # >> def __unicode__(self): # Python 3: def __str__(self): >> return self.name >> >> name = models.CharField(max_length=32) >> desc = models.CharField(max_length=254) >> img = models.CharField(max_length=32) >> user = models.ManyToManyField(User) >> components = models.ManyToManyField(component) >> category = models.ManyToManyField(Category) >> > Hello, > > 1. This code does not compiles, only to note in case you already didn't > tested > > 2. In your view: why do you instantiate a different form for POST request > and any other kind of requests: NewItemForm in one case, CategoryForm in > the rest. > > 3. In your view: You are using the string "none" instead the built-in > None. I recommend you to use Python built-in None. > > 4. ModelForm's save method actually saves the related many to many data. > Why are you doing these? > new_item.category.add(new_item) > new_item.components.add(new_item) > > You are adding a Item where a component and a Category is expected. This > does not make sense. You add(new_item) where you should be add(component()) > or something. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/33f75be6-615b-4556-a470-3be430074c85%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.