1

I have been able to save m2m relationships with forms in the past, but I am currently have problems with the following and I can't understand why:

# models.py

class File(models.Model):
    client = models.ManyToManyField(Client)
    product = models.ForeignKey(Product, related_name='item_product')
    created = models.DateTimeField(default=datetime.now)
    created_by = models.ForeignKey(User)

# forms.py

class FileForm(ModelForm):

    class Meta:
        model = File
        exclude = ('client')

    def CustomSave(self,product,client,user):
        temp = self.save(commit=False)
        temp.product = product
        temp.client = client   # < ?!?!
        temp.created_by = user
        temp.save()
        temp.save_m2m()
        return temp

# views.py

def new_client_view(request):

    if request.method == 'POST':
        try:
            i_product = int(request.POST['product'])
        except ValueError:
            raise Http404()
        # get a 'product' from the POST data
        p = Product.objects.get(id=i_product)

        formFile = FileForm(p, request.POST, auto_id='f_%s')
        formItemData = ItemDataForm(p, request.POST, auto_id='i_%s')

        if formFile.is_valid() and formItemData.is_valid():
            c = Client()
            c.save()
            tempFile = formFile.CustomSave(p,c,request.user)
            tempItem = ItemForm().CustomSave(tempFile,request.user)
            formItemData.CustomSave(tempItem,request.user)
            return redirect(client_view, c.id)
        else:
            return render_to_response('newClient.html', {'error':'The form was not valid'}, context_instance=RequestContext(request))
     else:
        return render_to_response('newClient.html', {}, context_instance=RequestContext(request))

When I try the above I get:

'File' instance needs to have a primary key value before a many-to-many relationship can be used.

And django indicates the error comes from temp.client = client

I have tried various permutations of the CustomSave without much success :(

Any ideas?

Robert Johnstone
  • 5,431
  • 12
  • 58
  • 88

2 Answers2

3

You're trying to assign one client to a ManyToMany field. This would work if it was a ForeignKey to a Client, but for ManyToMany, you will have to do something like:

temp.client.add(client)

Note: temp will have to be saved first, before you can assign clients. This is because there's a table Django uses mapping clients to files, and if the file doesn't first exist, you can't add a row w/ a null key. Looking at the table structure Django generates should help clear things up.

Dan Breen
  • 12,626
  • 4
  • 38
  • 49
  • I tried inserting that after the save and all I got was `'File' object has no attribute 'save_m2m'`. So I removed the `save_m2m()` and put it in the view as `formFile.save_m2m()` and got the same error :( – Robert Johnstone Oct 04 '11 at 17:58
  • `save_m2m` won't be needed here since we're saving it ourselves. – Dan Breen Oct 04 '11 at 18:06
  • I removed the `save_m2m()` and replace the `temp.client = client` with `temp.client.add(client)` and I still got `'File' instance needs to have a primary key value before a many-to-many relationship can be used.` – Robert Johnstone Oct 04 '11 at 18:37
  • sorry, put the `temp.client.add(client)` after the `temp.save()`. I got ya. My bag! – Robert Johnstone Oct 04 '11 at 18:40
2

You are trying to save the m2m relationship before the object itself has been saved (commit=False). To create the relationship between the client and the file, the file needs to be saved first.

See here: Django: instance needs to have a primary key value before a many-to-many relationship

Community
  • 1
  • 1
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177