ValueError:使用makemigrations时无法序列化函数:lambda

时间:2022-06-20 02:08:51

When I do python manage.py makemigrations, i get above error and I am unsure where the error is happening. I saw some post regarding this issue but i find mainly in DateTimeField() where the function was passed but in my case I have used auto_now attribute instead of some datetime related function.

当我做python manage.py makemigrations时,我得到了上述错误,我不确定错误发生在哪里。我看到一些关于这个问题的帖子,但我发现主要在DateTimeField()中传递了函数但在我的情况下我使用了auto_now属性而不是一些与datetime相关的函数。

However, I have used lambda function in the class method as follow.

但是,我在类方法中使用了lambda函数,如下所示。

@classmethod
    def get_content_models(cls):
        """
        Return all Package subclasses.
        """
        is_content_model = lambda m: m is not Package and issubclass(m, Package)


def set_helpers(self, context):
    current_package_id = getattr(current_package, "id", None)
    current_parent_id = getattr(current_package, "parent_id", None)
    self.is_current_child = self.parent_id == current_package_id
    self.is_child = self.is_current_child

    def is_c_or_a(package_id):
        parent_id = context.get("_parent_package_ids", {}).get(package_id)
        return self.id == package_id or (parent_id and is_c_or_a(parent_id))
    self.is_current_or_ascendant = lambda: bool(is_c_or_a(current_package_id))

I am not clear on this issue, So i have posted for understanding the cause. Is above code creating that issue? If it is the cause, what should be done instead?

我不清楚这个问题,所以我已经发布了解原因。以上代码是否创建了该问题?如果是原因,应该做什么呢?

I don't know where exactly this issue lies in the models so here is the detail of models.py of package app in gist because the code is a bit huge . It is not reached to booking models so I am only putting the code of package app and here it is

我不知道这个问题究竟在哪里,所以这里是gist中的包app的models.py的细节,因为代码有点大。它没有达到预订模型所以我只是把包app的代码放在这里

https://gist.github.com/SanskarSans/51d2f287309a97163e680cc38abd3e06

UPDATE

In my Package models, I have used the custom field and in that field there was the use of lambda instead of callable function and that was creating an issue. Due to the long file of models, I did not paste it here, I apologize for that.

在我的Package模型中,我使用了自定义字段,在该字段中使用了lambda而不是callable函数,这就产生了一个问题。由于模型的长文件,我没有在这里粘贴,我为此道歉。

Here what I had done

这就是我所做的

in_menus = MenusField(_("Show in menus"), blank=True, null=True)

class MenusField(MultiChoiceField):
    """
    ``MultiChoiceField`` for specifying which menus a package should
    appear in.
    """

    def __init__(self, *args, **kwargs):
        choices = [t[:2] for t in getattr(settings, "PAGE_MENU_TEMPLATES", [])]
        default = getattr(settings, "PAGE_MENU_TEMPLATES_DEFAULT", None)
        if default is None:
            default = [t[0] for t in choices]
        elif not default:
            default = None
        if isinstance(default, (tuple, list)):
            d = tuple(default)
            # this lambda should be avoided
            # default = lambda:d 
            default = default_value(d)
        defaults = {"max_length": 100, "choices": choices, "default": default}
        defaults.update(kwargs)
        super(MenusField, self).__init__(*args, **defaults)

1 个解决方案

#1


0  

I assume you are using the pickle module for serialization.

我假设您正在使用pickle模块进行序列化。

You cannot pickle the class in which set_helpers is defined. That method sets self.is_current_or_ascendant to a lambda function, and those are on the list of things that cannot be pickled (see 12.1.4 in https://docs.python.org/3/library/pickle.html).

你不能挑选定义了set_helpers的类。该方法将self.is_current_or_ascendant设置为lambda函数,并且这些函数位于无法进行pickle的事务列表中(请参阅https://docs.python.org/3/library/pickle.html中的12.1.4)。

The class method cannot be a problem since it only defines one local variable, is_content_model, which immediately goes out of scope and gets deleted. In fact that method, as you presented it here, does nothing at all.

类方法不是问题,因为它只定义了一个局部变量is_content_model,它立即超出范围并被删除。事实上,正如你在这里提出的那样,这种方法什么都不做。

#1


0  

I assume you are using the pickle module for serialization.

我假设您正在使用pickle模块进行序列化。

You cannot pickle the class in which set_helpers is defined. That method sets self.is_current_or_ascendant to a lambda function, and those are on the list of things that cannot be pickled (see 12.1.4 in https://docs.python.org/3/library/pickle.html).

你不能挑选定义了set_helpers的类。该方法将self.is_current_or_ascendant设置为lambda函数,并且这些函数位于无法进行pickle的事务列表中(请参阅https://docs.python.org/3/library/pickle.html中的12.1.4)。

The class method cannot be a problem since it only defines one local variable, is_content_model, which immediately goes out of scope and gets deleted. In fact that method, as you presented it here, does nothing at all.

类方法不是问题,因为它只定义了一个局部变量is_content_model,它立即超出范围并被删除。事实上,正如你在这里提出的那样,这种方法什么都不做。