gpt4 book ai didi

python - Django Model 类中的函数不接受 self 参数?

转载 作者:行者123 更新时间:2023-12-01 04:11:04 25 4
gpt4 key购买 nike

我在以下代码中收到“TypeError:get_query() 恰好需要 1 个参数(给定 0 个)”:

class graph_column_format(models.Model):

def get_query(self):
return self.graph_id.query_id

graph_id = models.ForeignKey("graph", on_delete=models.CASCADE)
column = models.ForeignKey("query_column", on_delete=models.CASCADE,
limit_choices_to={"query_id": get_query()})

定义在类中,所以我不明白为什么会收到此错误。

最佳答案

如果您有该模型类的实例,则只能调用该方法,例如

< graph_column_format instance >.get_query()

但是无法以这种方式使用 limit_choices_to ,但您可以在 modelForm 构造函数中添加过滤器,例如,

假设您在 graph_column_format 模型上有 query_id 字段

class graph_column_format_form(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(MyOrderForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['query_id'].queryset = graph_column_format.objects.filter(query_id=self.instance.graph_id.query_id)

关于python - Django Model 类中的函数不接受 self 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34945625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com