开发者

Django problems with saving a m2m relationship

开发者 https://www.devze.com 2023-04-10 09:19 出处:网络
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:

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' 开发者_StackOverflow中文版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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消