在Django中作为过滤器的参数的函数

huangapple go评论76阅读模式
英文:

function as parameter of a filter in django

问题

#模型
class Bens(models.Model):
    identificador = models.CharField(max_length=100, primary_key=True) # Série/Chassi
    codigo = models.CharField(unique=True, max_length=10)
    modelo = models.CharField(max_length=50)

    def dentrorev(self): 
        ultimo_uso = Usos.objects.filter(bem=self.codigo).order_by('data').first()
        margem_atual = ultimo_uso.valor % self.periodoRevisao

        if margem_atual >= (self.periodoRevisao - self.margemRevisao) and margem_atual <= self.periodoRevisao or margem_atual >= 0 and margem_atual <= self.margemRevisao:
            return True
        else:
            return False

    def forarev(self):
        bensUsos = Bens.objects.filter(codigo__in=[x['ordemServico__uso__bem__codigo'] for x in OrdensServicos.objects.exclude(listaStatus='4').order_by('ordemServico__uso__bem__codigo').values('ordemServico__uso__bem').distinct().values('ordemServico__uso__bem__codigo')])

        if self.codigo in bensUsos:
            return False
        else:
            return True

#视图
revisao = Bens.objects.filter(dentrorev=True).union(Bens.objects.filter(forarev=True))
英文:

could someone take a doubt about filter using django, I have a class and in this class I have a function, and I wanted to pass this function as a parameter inside a filter in django, would it be possible?

#model
class Bens(models.Model):
	identificador = models.CharField(max_length=100, primary_key=True) # S&#233;rie/Chassi
	codigo = models.CharField(unique=True, max_length=10)
	modelo = models.CharField(max_length=50)

	def dentrorev(self): 
		ultimo_uso = Usos.objects.filter(bem=self.codigo).order_by(&#39;-data&#39;).first()
		margem_atual = ultimo_uso.valor%self.periodoRevisao

		if(margem_atual&gt;=(self.periodoRevisao-self.margemRevisao) and margem_atual &lt;=(self.periodoRevisao) or margem_atual&gt;=0 and margem_atual&lt;=(self.margemRevisao)):
			return True
		else:
			return False
		
	def forarev(self):

		bensUsos = Bens.objects.filter(codigo__in = [x[&#39;ordemServico__uso__bem__codigo&#39;] for x in OrdensServicos.objects.exclude(listaStatus=&#39;4&#39;).order_by(&#39;ordemServico__uso__bem__codigo&#39;).values(&#39;ordemServico__uso__bem&#39;).distinct().values(&#39;ordemServico__uso__bem__codigo&#39;)])

		if self.codigo in bensUsos:
			return False
		else:
			return True

#view
revisao = Bens.objects.filter(dentrorev=True).union(Bens.objects.filter(forarev=True))

答案1

得分: 0

Django ORM 在数据库层面操作 - 您需要首先获取一个实例来评估该方法。您可以为您的模型类编写自定义管理器以进行某些解决方法,但在代码不是用英语编写时很难提供示例。

英文:

No, Django ORM operates at the database level - you would have to fetch an instance first to evaluate the method. You can write a custom Manager for your Model class for some workaround, but it is hard to provide an example when the code is not written in English.

答案2

得分: 0

我曾经面临过一个非常类似的情况,我既不能通过计算字段(就像你一样),也不能通过annotate来解决它。我的解决方案是:

  1. 在表中创建新的“真实”字段(在你的情况下是'dentrorev'和'forarev')。
  2. 将这些函数放入视图中(或者在我的情况下从视图中调用它们),并修改它们以迭代数据集并将计算出的值写入数据库。

如果你仔细思考一下,从性能角度来看,这并没有太大的不同,因为计算字段除了写入数据库外,其余都是一样的。

英文:

I did face a very similar situation and I could't solve it neither with calculated fields (like you) nor with annotate. My solution was:

  1. Create new "real" fields in the table ('dentrorev' & `forarev' in your case).
  2. put those functions in the view (or call them from the view in my case) and modify them to iterate through the dataset and write the calculated values to the DB.

If you think it carefully, it is not very diferent in terms of performance, since the calculated field is doing the same except for writing to the DB.

huangapple
  • 本文由 发表于 2023年7月6日 21:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定