英文:
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é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
#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
来解决它。我的解决方案是:
- 在表中创建新的“真实”字段(在你的情况下是'dentrorev'和'forarev')。
- 将这些函数放入视图中(或者在我的情况下从视图中调用它们),并修改它们以迭代数据集并将计算出的值写入数据库。
如果你仔细思考一下,从性能角度来看,这并没有太大的不同,因为计算字段除了写入数据库外,其余都是一样的。
英文:
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:
- Create new "real" fields in the table ('dentrorev' & `forarev' in your case).
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论