简体   繁体   中英

Django - how to get the field value before saving?

I'm new with Django and I've been trying to get the value from a model field before I save it, for example:

class Student(models.Model):
    name = models.CharField(max_length=30, null=False)
    id = models.CharField(max_length=30, null=False, primary_key=True)
    phone = models.BigIntegerField(max_length=50, null=False)


def print_name():
    #here I want to catch and print the student's name before it saves

Just override the save method inside your model.

class Student(models.Model):
    name = models.CharField(max_length=30, null=False)
    id = models.CharField(max_length=30, null=False, primary_key=True)
    phone = models.BigIntegerField(max_length=50, null=False)

    def save(self):
        print self.name
        super(Student, self).save()

For some reason, none of the answers worked for me.

I was still able to solve the problem by retrieving the object from the database and comparing it against the version being saved:

def save(self, *args, **kwargs):
    old_object_instance = YourModel.objects.get(pk=self.pk)
    if old_object_instance.your_value != self.your_value:
        do_something_here()
    super().save(*args, **kwargs)

You can achieve that by overriding the default save method like this.

class Student(models.Model):
    name = models.CharField(max_length=30, null=False)
    id = models.CharField(max_length=30, null=False, primary_key=True)
    phone = models.BigIntegerField(max_length=50, null=False)
    def save(self, *args, **kwargs):
        print self.name
        super(Student, self).save(*args, **kwargs)

It does not really matter if it's in save() method or in any other method within the model class, you can access any model attribute with self which refers to the current instance:

class Student(models.Model):
    name = models.CharField(max_length=30, null=False)
    id = models.CharField(max_length=30, null=False, primary_key=True)
    phone = models.BigIntegerField(max_length=50, null=False)

    def any_function(self, any_arg1, any_arg2):
       ...  
       print self.name
       return self.name

Here i give you another solution using pre_save Signal

from django.db.models.signals import pre_save
from django.dispatch import receiver

@receiver(pre_save, sender=YourModel)
def my_callback(sender, instance, *args, **kwargs):
    print instance.name

Depending on your needs, previous answers could or could not work.

If you use this:

def save(self, *args, **kwargs):
    print(self.name)
    super(self).save(*args, **kwargs)

This only will work if your are editing a previously saved object. If you are creating a new object and want to get a field value before saving, you need to do this (will also work in case you are editing).

def save(self, *args, **kwargs):
    print(self._meta.get_field('name').value_from_object(self))
    super(Student, self).save(*args, **kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM