[Django]-Updating foreign key field in Django

2👍

I managed to tweak the update example from https://docs.djangoproject.com/en/dev/topics/db/queries/#saving-foreignkey-and-manytomanyfield-fields. This seems to be working as desired.

    def formset_valid(self, formset):

        self.object = formset.save(commit=False)
        revdefault = RevisionDefaultType.objects.get(pk=1)
        revget = RevisionSettings.objects.get(global_revision_type=self.object)
        revdefault.defaultrevisiontype = revget
        revdefault.save()

        return HttpResponseRedirect(self.get_success_url())

Thanks again for the help.

👤Karl

2👍

You must add force_update=True to your save() function.

For more info about How Django knows to UPDATE vs. INSERT see this link in django’s documentation.

👤Mazdak

Leave a comment