Hi,
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)

-- 
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/dc51e07e-3bdc-49ed-a8e4-c5a4137e96bf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to