Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否将参数属性用作其他参数的默认值?_Python_Django - Fatal编程技术网

Python 是否将参数属性用作其他参数的默认值?

Python 是否将参数属性用作其他参数的默认值?,python,django,Python,Django,python中有没有办法将一个参数的默认值设置为另一个参数的属性 乙二醇 我目前在django中有一个函数,我想这样做 def supplier_default_product(region, supplier=region.default_supplier.id): default_product_instance = Product.objects.get(name=default_product, supplier=supplier) .... 因此,我可以将其称为无供应

python中有没有办法将一个参数的默认值设置为另一个参数的属性

乙二醇

我目前在django中有一个函数,我想这样做

def supplier_default_product(region, supplier=region.default_supplier.id):
    default_product_instance = Product.objects.get(name=default_product, supplier=supplier)
    ....
因此,我可以将其称为无供应商,其中仅使用区域默认供应商:

supplier_default_product(region)
supplier_default_product(region, supplier)
或与指定的供应商:

supplier_default_product(region)
supplier_default_product(region, supplier)

使用
None
作为默认参数,然后在函数体中检查
None

def supplier_default_product(region, supplier=None):
    if supplier is None:
        supplier = region.default_supplier.id
    default_product_instance = Product.objects.get(name=default_product, supplier=supplier)

您不能引用另一个参数的属性,因为默认参数值是在定义时间设置的,只发生一次。

谢谢您解释为什么它不起作用。@Yunti没问题。很高兴我能帮忙!