Viral Joshi Viral Joshi - 2 months ago 8
MySQL Question

How can I filter exported tickets from database using Django?

I am working on a Django based web project where we handle tickets based requests. I am working on an implementation where I need to export all closed tickets everyday.

My ticket table database looks like,

-------------------------------------------------
| ID | ticket_number | ticket_data | is_closed |
-------------------------------------------------
| 1 | 123123 | data 1 | 1 |
-------------------------------------------------
| 2 | 123124 | data 2 | 1 |
-------------------------------------------------
| 3 | 123125 | data 3 | 1 |
-------------------------------------------------
| 4 | 123126 | data 4 | 1 |
-------------------------------------------------


And my ticket_exported table in database is similar to

----------------------------------
| ID | ticket_id | ticket_number |
----------------------------------
| 10 | 1 | 123123 |
----------------------------------
| 11 | 2 | 123124 |
----------------------------------


so my question is that when I process of exporting tickets, is there any way where I can make a single query to get list of all tickets which are closed but
ticket_id
and
ticket_number
is not in
ticket_exported
table? So when I run functions it should get tickets with
ticket_id
'3' and '4' because they are not exported in
ticket_export
database.

I don't want to go through all possible tickets and check one by one if their id exists in exported tickets table if I can just do it in one query whether it is raw SQL query or Django's queries.

Thanks everyone.

Answer

Per my comment- you could probably save yourself a bunch of trouble and just add another BooleanField for 'is_exported' instead of having a separate model assuming there aren't fields specific to TicketExported.

@doniyor's answer gets you the queryset you're looking for though. In response to your raw SQL statement question: you want: unexported_tickets.query.