get id from FORM save

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.

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.)

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?

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

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

models.py:


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.

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()

Hi,

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

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

models.py

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

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()

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).

Just to address one of your points, form.save() does return the instance. See the Django code here; or even in v3.0.4 here.

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).