0

I have a form which asks a user to add a category for a file they are uploading, which is a ManytoManyField. However, the values of this field are NOT being added to the database, and I cannot figure out why. The variable in the code below is descategories

<form enctype="multipart/form-data" method="POST" action="" class="uniForm">
        <fieldset class="inlineLabels">
            {% csrf_token %}
            <table>
            <tr>
            <td>Design Title:</td><td>{{ formtoadddesign.modelname}}</td></td></tr>
            <tr><td>Author:</td> <td>{{ formtoadddesign.author }}</td></tr>
            <tr><td>Description:</td> <td>{{ formtoadddesign.description }}</td></tr>
            {% comment %}<tr><td>Design Image:&nbsp&nbsp&nbsp</td><td>{{ formtoadddesign.partimage }}</td></tr>{% endcomment %}
            <tr><td>Tags:</td><td>{{formtoadddesign.tags}}</td></tr>
            <tr><td>Format:</td><td>{{formtoadddesign.format}}</td></tr>
            <tr><td>Category:</td><td>{{formtoadddesign.descategories}}</td></tr>
            <tr><td>Attach File</td><td>{{formtoadddesign.content}}</td></tr>
                    </div>
            </table>
            <br>
            <div class="form_block">
                <input type="submit" class="btn btn-success" value="{% trans 'Add Design' %}">
            </div>
            <br>
        </fieldset>
    </form>

I have the following code in my views.py:

def add_design (request):
    # POST request  
    if request.method == "POST":
        formtoadddesign = designform(request.POST, request.FILES)
        if formtoadddesign.is_valid():
            new_design = formtoadddesign.save(commit=False)
            new_design.adder = request.user
            designname = new_design.modelname
            format = new_design.format
            adder_id = new_design.adder.id
            file = request.FILES['content']
            filename = file.name
            filecontent = file.read()
            file.seek(0)
            new_design.content=store_in_s3_design(filename, filecontent, designname, format, adder_id)
            new_design.save()
            cats = new_design.descategories
            print "new_design id:", repr(new_design.id)
            print cats
            for cat in cats.all():
                new_design.designcategory_id.add(cat)
                print "added %s" % cat

Basically I can't seem to get get the values select from the ManytoManyField which is basically a list in the form people can select from. How do I access these values -- cats.all() returns nothing, so I must be doing something wrong...

Here's my form:

class designform(ModelForm):
    def __init__(self, *args, **kwargs):
        super(designform, self).__init__(*args,**kwargs)
        self.is_update=False

    def clean(self):
        """
        Do Validation stuff
        """
        if 'modelname' not in self.cleaned_data:
                return
        if not self.is_update:
            return self.cleaned_data

    class Meta:
        model = Design
1
  • Can you post your form as well Commented Nov 25, 2012 at 21:25

1 Answer 1

1

since you are using save(commit=False) you need save_m2m

see https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.