简体   繁体   English

Django - 如何在保存之前获取字段值?

[英]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:我是 Django 的新手,我一直在尝试从模型字段中获取值,然后再保存它,例如:

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.只需覆盖模型中的save方法。

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.您可以通过覆盖这样的默认save方法来实现这一点。

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:它是在save()方法中还是在模型类中的任何其他方法中并不重要,您可以使用引用当前实例的self访问任何模型属性:

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在这里我给你另一个使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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