# get id from FORM save

**URL:** <https://forum.djangoproject.com/t/get-id-from-form-save/10482>\
**Category:** Using Django\
**Created:** [November 11, 2021, 6:41pm UTC](https://forum.djangoproject.com/t/get-id-from-form-save/10482 "2021-11-11T18:41:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danny-katalun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danny-katalun/32/6340_2.png) [@danny-katalun](https://forum.djangoproject.com/u/danny-katalun)\
**Post date:** [November 11, 2021, 6:41pm UTC](https://forum.djangoproject.com/t/get-id-from-form-save/10482/1 "2021-11-11T18:41:50Z")

</div>

Hello,

I am creating a new contact and I need to get its id/pk after saving.  
I tried :

new\_contact = form.save()  
clubform.contact\_id = new\_contact.id or new\_contact.pk or just new contact

non work . I get:  
null value in column “contact\_id” violates not-null constraint

I tried overriding the save method in the model:  
def save(self, \*args, \*\*kwargs):  
super(Contact, self).save(\*args, \*\*kwargs)  
return self

so, how can I get the id?

I am using django 3.04

Thanks.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 12, 2021, 2:25am UTC](https://forum.djangoproject.com/t/get-id-from-form-save/10482/2 "2021-11-12T02:25:57Z")

</div>

It’ll help to see the model, view, and form involved here.

(When posting code, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your python code properly formatted.)

> [@danny-katalun](#):
>
> ```auto
> def save(self, *args, **kwargs):
> super(Contact, self).save(*args, **kwargs)
> return self
> 
> ```

This does nothing for you. You’re creating a function which calls super, which is what would happen anyway if you didn’t supply this function. What are you trying to accomplish by this?

---

<div class="post-metadata">

**Author:** ![danny-katalun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danny-katalun/32/6340_2.png) [@danny-katalun](https://forum.djangoproject.com/u/danny-katalun)\
**Post date:** [November 12, 2021, 6:16am UTC](https://forum.djangoproject.com/t/get-id-from-form-save/10482/3 "2021-11-12T06:16:18Z")

</div>

Hi,

regarding the save function.  
I am getting null from form.save().  
so,I was following this blog for returning the instance after save so I will have the id:  
[https://www.py4u.net/discuss/1269910](https://www.py4u.net/discuss/1269910)

“override the save method of your model and return the instance after the super call.”

models.py:

```auto

class Contact(models.Model):
    owner = models.CharField(max_length=36,default="0",blank=True,null=True)
    fname = models.CharField(max_length=36,null=False)
    lname = models.CharField(max_length=36,null=False)
    birthdate = models.DateField(blank=True,null=True)
    address = models.CharField(max_length=100,blank=True,null=True)
e, null=False, blank=False)
    last_modified = models.DateTimeField(auto_now=True, editable=False, null=False, blank=False)

    def save(self, *args, **kwargs):
        super(Contact, self).save(*args, **kwargs) 
        return self

    def __str__ (self):
        fullname = self.fname + " " + self.lname
        return fullname

class ClubMember(models.Model):
    contact = models.ForeignKey(Contact,on_delete=models.CASCADE,related_name='ContactClub')
    status = models.BooleanField(default=False)
    joindate = models.DateField(blank=True,null=True)
    expiredate = models.DateField(blank=True,null=True)
    accumulation = models.PositiveIntegerField(default=1)
    created = models.DateTimeField(auto_now_add=True, editable=False, null=False, blank=False)
    last_modified = models.DateTimeField(auto_now=True, editable=False, null=False, blank=False)

```

views.py

adding new contact is quite long . I’ll post the relevant part.

```auto
cform = newContact(request.POST)
form = cform.save(commit=False)
# some long code not relevant
new_contact = form.save()
if request.POST.get('member'):
clubform = clubForm()
clubform.contact_id = new_contact.id
clubform.status = True
clubform.joindate = request.POST.get('joindate')
clubform.save()

```

---

<div class="post-metadata">

**Author:** ![danny-katalun](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danny-katalun/32/6340_2.png) [@danny-katalun](https://forum.djangoproject.com/u/danny-katalun)\
**Post date:** [November 12, 2021, 6:26am UTC](https://forum.djangoproject.com/t/get-id-from-form-save/10482/4 "2021-11-12T06:26:53Z")

</div>

Hi,

regarding the save override :  
I am getting null from form.save().  
so, I followed this blog advice:  
[https://www.py4u.net/discuss/1269910](https://www.py4u.net/discuss/1269910)

“override the save method of your model and return the instance after the super call.”

models.py

```auto
class Contact(models.Model):
    owner = models.CharField(max_length=36,default="0",blank=True,null=True)
    fname = models.CharField(max_length=36,null=False)
    lname = models.CharField(max_length=36,null=False)
    created = models.DateTimeField(auto_now_add=True, editable=False, null=False, blank=False)
    last_modified = models.DateTimeField(auto_now=True, editable=False, null=False, blank=False)

    def save(self, *args, **kwargs):
        super(Contact, self).save(*args, **kwargs) 
        return self

    def __str__ (self):
        fullname = self.fname + " " + self.lname
        return fullname

class ClubMember(models.Model):
    contact = models.ForeignKey(Contact,on_delete=models.CASCADE,related_name='ContactClub')
    status = models.BooleanField(default=False)
    joindate = models.DateField(blank=True,null=True)
    expiredate = models.DateField(blank=True,null=True)
    accumulation = models.PositiveIntegerField(default=1)
    created = models.DateTimeField(auto_now_add=True, editable=False, null=False, blank=False)
    last_modified = models.DateTimeField(auto_now=True, editable=False, null=False, blank=False)

```

views.py

```auto
cform = newContact(request.POST)
form = cform.save(commit=False)
# some long not relevant code.. 
new_contact = form.save()
clubform = clubForm()
clubform.contact_id = new_contact.id
clubform.status = True
clubform.joindate = request.POST.get('joindate')
clubform.save()

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 12, 2021, 5:18pm UTC](https://forum.djangoproject.com/t/get-id-from-form-save/10482/5 "2021-11-12T17:18:49Z")

</div>

> [@danny-katalun](#):
>
> I am getting null from form.save().  
> so, I followed this blog advice:  
> [https://www.py4u.net/discuss/1269910](https://www.py4u.net/discuss/1269910)

I read that thread. Both the description is poor, and the answer misunderstands the real issue and the problem caused by it. The person wrote a method on a class and forgot to specify that the first parameter received is `self`. The workarounds and the answer are, uh, suboptimal at best. (Trying to create an instance of the model from within the model class is also not the right approach.)

Your save method is doing nothing.

Regarding your specific issue, we need to see the _complete_ view. When a view is concerned, there’s no such thing as “not relevant code”.

You also haven’t provided the form(s).

---

<div class="post-metadata">

**Author:** ![awtimmering](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/awtimmering/32/4109_2.png) [@awtimmering](https://forum.djangoproject.com/u/awtimmering)\
**Post date:** [November 13, 2021, 6:49am UTC](https://forum.djangoproject.com/t/get-id-from-form-save/10482/6 "2021-11-13T06:49:06Z")

</div>

Just to address one of your points, `form.save()` _does_ return the instance. See the Django code [here](https://github.com/django/django/blob/main/django/forms/models.py#L472); or even in v3.0.4 [here](https://github.com/django/django/blob/3.0.4/django/forms/models.py#L465).

So if your form is not returning an id that means there is some other issue, that we can’t guess at without seeing more code (as per Ken’s replies).
