I have a model with a custom save function, where I want to perform certain functions based on a condition like this:
class ClassName(models.Model):
def save(self, *args, **kwargs):
reindex = **kwargs.pop("reindex")
super().save(*args, **kwargs)
if reindex:
People.objects.create()
Now inside a task I want to call the following:
kwargs = { "reindex": False}
ClassName.objects.get_or_create(**kwargs)
When it does a create, it obviously runs the save function, but it's giving me an error saying reindex is not a field
. I have been researching it for a while now and can't figure out what to do. Maybe someone can point me in the right direction.
I just want to pass in an argument into the get_or_create
, so that I can conditionally perform a certain function in the save method.
When you do
kwargs = { "reindex": False}
ClassName.objects.get_or_create(**kwargs)
it is actually equivalent to
ClassName.objects.get_or_create(reindex=False)
Thus, since reindex
appears not to be a field defined in the model ClassName
, you get an error.
reindex = **kwargs.pop("reindex")
, you should define reindex
as one of the fields of your model.
But I admit that I answer blindly, because to me, your class definition cannot work like so.
If one assumes that reindex is an integer field, you could do
class ClassName(models.Model): reindex = models.IntegerField(null=True) def save(self, *args, **kwargs): super(ClassName, self).save(*args, **kwargs) if "reindex" in kwargs: People.objects.create()
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.