Delete a row and check if the rest is not empty

Hello.

This is the model, which I take as an example:

Models.py

class orderEmergencyList(models.Model):
    STATUS = [
        ("CE","EMAIL SENDS"),
        ("CR","EMAIL RECEIVED"),
        ("OA","ORDER ACCEPTED"),
        ("OR","ORDER REFUSED"),
        ("OD","ORDER OFF"),
    ]

    ticket = models.IntegerField(default=1000, blank=True) 
    email=models.EmailField(blank=True)
    status = models.CharField(max_length=3,choices= STATUS,default= "SO")

What I have to do is look for a provider from the database OrdenEmergenciaLista by ticket and email.
And then delete it.

order=orderEmergencyList.objects.filter(ticket=ticket_).filter(email=email_).first()
order.delete()

This is correct? or should I perform an order.save()

Then I need to know how many rows in the orderEmergencyList database there are with ticket=ticket_

order=orderEmergencyList.objects.filter(ticket=ticket_)
return len(order)

Is that way correct, by using the len function?

This can also be written as:
order=orderEmergencyList.objects.filter(ticket=ticket_, email=email_).first()

This will delete that entry.

No. That forces the query to execute and to create the list of objects for the entries in the table. If you’re not otherwise going to process all the individual elements of the list, you don’t want to do that.

The preferred solution is to use the count() method.

1 Like