I need to do a delete and update based on status.
Here are models:
class Purchases(TimeStampedModel):
APROVADO = "AP"
EM_VALIDACAO = "VA"
STATUS_CHOICHES = (
(APROVADO, "Aprovado"),
(EM_VALIDACAO, "Em validação"),
)
values = models.DecimalField(decimal_places=2, max_digits=10, default=0)
cpf = BRCPFField("CPF")
status = models.CharField(max_length=20, choices=STATUS_CHOICHES, default=EM_VALIDACAO)
I'm trying to do like this in my viewset:
def get_queryset(self):
qs = super().get_queryset()
if self.action in ("update", "parcial_update", "delete"):
qs.filter(Purchases.status=="VA")
return qs
However he is still letting edit the orders with the approved status. Can only be deleted or edited purchase with status "In validation"
Can someone help me?
qs.filter(…) [Django-doc] does not filter the queryset qs. It construct a new queryset that is the filtered variant of qs.
You thus should return the filtered queryset, or assign the result to qs:
def get_queryset(self):
qs = super().get_queryset()
if self.action in ('update', 'parcial_update', 'delete'):
return qs.filter(Purchases.status=='VA')
return qs