Wednesday, May 30, 2018

Re: How to get the primary key of the underlying model in a ModelForm?

Mathew,

Thanks!

Mark

On Tue, May 29, 2018 at 2:18 PM, Matthew Pava <Matthew.Pava@iss.com> wrote:

You can access self.instance, which should have all the fields populated for that model instance.  So you can access it via self.instance.pk.

 

Keep in mind, though, that if you are creating the Document, you will not have a pk until after the save is committed, so it won't be accessible in the clean method.  If you are updating a document, then pk will populated.

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Mark Phillips
Sent: Tuesday, May 29, 2018 4:15 PM
To: django users
Subject: How to get the primary key of the underlying model in a ModelForm?

 

I have a model, Document, and an associated DocumentForm(forms. ModelForm), and a DocumentAdmin(admin.ModelAdmin). In the clean method of the DocumentForm, I need to get the document_id (ie primary key) of the underlying Document, but it is not included in the field set for that form. I need the primary key, because one of the fields on the form belongs to another model (it is a JSON field), and I need to save that field in the other model when the DocumentForm is saved.

 

How do I access the primary key of the underlying Model in a ModelForm?

 

Thanks!

 

Mark

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEqej2OAvc78CgykeYYs5Wvc-MPQRR0Lnhp5NYQt3hpVf_cv8w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1a98417972974d919030e6a5ed76cced%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEqej2PT6C-PiM45nJD2%3D6GyFwXWspYKMJxVE8f4EJyaVTDQYw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment