Reputation: 1283
I have 2 objects contacts
, companies_contact
, I am using distinct() to get distinct value from contact
the code looks like this:
contacts = Contact.objects.filter(campaign=campaign)
companies_contacts = contacts.distinct('website')
So, when I iterate the companies_contact
I'm getting the following output...
>>>for i in companies_contacts:
i.created_at, i.website
(datetime.datetime(2018, 1, 9, 10, 57, 40, 447445, tzinfo=<UTC>), 'www.creamstone.com')
(datetime.datetime(2018, 1, 19, 6, 27, 32, 758898, tzinfo=<UTC>), 'www.facebook.com')
(datetime.datetime(2018, 1, 18, 6, 20, 41, 145358, tzinfo=<UTC>), 'www.heteja.com')
(datetime.datetime(2018, 1, 9, 12, 11, 17, 390755, tzinfo=<UTC>), 'www.kfc .com')
(datetime.datetime(2018, 1, 31, 6, 44, 40, 916231, tzinfo=<UTC>), 'www.mvg.com')
(datetime.datetime(2018, 1, 11, 12, 20, 55, 409986, tzinfo=<UTC>), 'www.subway.com')
(datetime.datetime(2018, 1, 9, 9, 14, 58, 607180, tzinfo=<UTC>), 'www.websit.com')
(datetime.datetime(2018, 1, 9, 6, 29, 53, 270203, tzinfo=<UTC>), 'www.website.com')
(datetime.datetime(2018, 1, 9, 9, 22, 22, 869395, tzinfo=<UTC>), 'www.websitest.com')
So, according to my understating companies_contact
consists of this much data only but when I'm applying filter()
on companies_contact
with the different date which is not in above output then also it gives me the result.
companies_contacts.filter(created_at__startswith='2018-02-01')
The above query giving me the result but this created_at
value is not there when I have iterate companies_contact
I don't know why it is giving result and why it is working but I don't want result if the dates are not there.
Upvotes: 1
Views: 133
Reputation: 56
I'm not sure what are you trying to do, so I'm guessing that maybe is because of the startswith tag inside the of the filter. What if you change startswith tag with gte and lt:
1. Parse '2018-02-01' to datetime object.
date_obj = parse('2018-02-01')
2. Filter with greater than equal and less than.
companies_contacts.filter(
created_at__gte=date_obj,
created_at__lt=date_obj + relativedelta(days=+1)
)
I'm using dateutil package:
from dateutil.parser import parse
from dateutil.relativedelta import relativedelta
Upvotes: 1