21:55 PM
lvns_ joined the channel
21:56 PM
deltaskelta joined the channel
21:56 PM
MikiSoft
21:56 PM
but i don't understand clearly how to implement it
21:57 PM
airstrike
iniglo: no problem! sorry I couldn't be more helpful
21:57 PM
kyheo has quit
21:58 PM
kyheo joined the channel
22:00 PM
deltaskelta has quit
22:00 PM
cewing has quit
22:01 PM
robvdl joined the channel
22:02 PM
kyheo has quit
22:02 PM
aossama joined the channel
22:02 PM
MikiSoft: that's very clever but I think going down the prefetch route might be easier
22:02 PM
dcrouch joined the channel
22:02 PM
MikiSoft
yeah this is too complicated for me
22:03 PM
how to do a prefetch?
22:03 PM
i should first query Like objects and then pull from them Event ones, right?
22:05 PM
airstrike
I haven't done this in years so I wouldn't be the best person to guide you
22:05 PM
ytsejam
sorry to repeat simple question how to edit datetime created and updated fields according to create and update with datetime widget ?
https://dpaste.de/r4oR
22:06 PM
airstrike
22:07 PM
MikiSoft
hmm
22:07 PM
Event.objects.prefetch_related('like')
22:07 PM
this doesn't work
22:07 PM
maybe i have to add a field 'likes' in Event?
22:08 PM
which is a generic relation to Like model
22:08 PM
airstrike
Event.objects.all().prefetch_related('content_object')
22:08 PM
check the UPDATE: part of the answer
22:08 PM
cnk
You need to do Like.objects.filter(personthing).prefetch_related('events')
22:09 PM
ytsejam: If you define your fields with auto_now or auto_now_add django will not let you edit them.
22:09 PM
MikiSoft
Like.objects.filter(person=User.objects.first()).prefetch_related('events')
22:09 PM
returns me of course Like object
22:09 PM
pardon me, this: Like.objects.filter(person=User.objects.first()).prefetch_related('content_object')
22:10 PM
cnk
yes - but you can loop through them and collect the events
22:10 PM
MikiSoft
if i put 'events' it doesn't work
22:10 PM
cnk
OK
22:10 PM
MikiSoft
it's the same as: Like.objects.filter(person=User.objects.first())
22:10 PM
ytsejam
cnk : both will be simply models.DateTimeField() that is all . am I right that ?
22:10 PM
cnk
yes
22:10 PM
MikiSoft
so what's the difference between the query with `prefetch_related` and without it?
22:11 PM
cnk
but you will need to fill them in in your forms or they will end up complaining they can't be blank / null
22:11 PM
prefetch_related does a second query for all hte related events
22:11 PM
MikiSoft
oh
22:12 PM
cnk
so if you loop through the likes to collect the events, they are in memory and don't do n queries to retrieve the events
22:12 PM
MikiSoft
i get it now
22:12 PM
thanks
22:13 PM
lit has quit
22:13 PM
cnk
22:13 PM
write the SQL you need and then call Event.objects.raw(SQLHERE)
22:13 PM
airstrike
I recommend reading through prefetch_related() and Prefetch() docs, and enabling logging of database queries so that you can check for performance
22:14 PM
22:14 PM
you can *probably* do this without raw SQL
22:14 PM
MikiSoft
but how
22:14 PM
airstrike
by reading the docs
22:14 PM
MikiSoft
also i don't know so well SQL
22:14 PM
airstrike
you can't do django without knowing SQL
22:14 PM
you're going to hit a wall sooner or later
22:15 PM
MikiSoft
what would be a SQL command for this?
22:15 PM
airstrike
the ORM is an abstraction layer, but it's very important to know what it is doing
22:15 PM
you can get the SQL with .query
22:15 PM
Like.objects.filter(person_id=1).prefetch_related('content_object').query
22:16 PM
cnk
airstrike: will that show both queries? or only the one on Like?
22:16 PM
MikiSoft
i can use something like this: print(Event.objects.filter(business__manager=User.objects.first()).query)
22:16 PM
airstrike
good question
22:16 PM
MikiSoft
it returns me
22:16 PM
SELECT "main_event"."id", "main_event"."business_id", "main_event"."text", "main_event"."when" FROM "main_event" INNER JOIN "main_business" ON ("main_event"."business_id" = "main_business"."id") WHERE "main_business"."manager_id" = 2
22:16 PM
airstrike
cnk: not sure
22:17 PM
MikiSoft
>>> print(Event.objects.all().prefetch_related('content_object').query)
22:17 PM
SELECT "main_event"."id", "main_event"."business_id", "main_event"."text", "main_event"."when" FROM "main_event"
22:17 PM
cyphase joined the channel
22:17 PM
oh wait
22:18 PM
>>> print(Like.objects.filter(person=User.objects.first()).prefetch_related('content_object').query)
22:18 PM
SELECT "main_like"."id", "main_like"."person_id", "main_like"."content_type_id", "main_like"."object_id", "main_like"."is_dislike" FROM "main_like" WHERE "main_like"."person_id" = 1
22:19 PM
squeaky-clean has quit
22:19 PM
i prefer doing it in ORM (django) way
22:19 PM
rub3nc joined the channel
22:19 PM
22:20 PM
i would go by that way
22:20 PM
or using prefetch_related
22:20 PM
airstrike
you need to try a few things
22:20 PM
go through the frustration of not knowing the answer
22:20 PM
MikiSoft
i don't know which of that two is better
22:20 PM
airstrike
and then finally figuring it out
22:20 PM
otherwise you'll never learn
22:20 PM
and your goal should be to learn not the solution, not just solve this one issue
22:21 PM
MikiSoft
the first solution isn't noob friendly unfortunately
22:21 PM
slick666_work has quit
22:21 PM
blackcross
the best solution to any problem is knowing how to properly find the solution
22:21 PM
MikiSoft
so i can't wrap the head around it
22:21 PM
airstrike
the first solution creates an on-the-fly class through a factory function called subclass_for_content_type()
22:21 PM
blackcross
22:22 PM
hutch34 joined the channel
22:22 PM
jarshwah has quit
22:22 PM
MikiSoft
the first one looks complicated to me
22:22 PM
maybe it's not
22:23 PM
but after implementing i can use filtering on generic relations
22:23 PM
which is good
22:23 PM
airstrike
google 'generic relations prefetch_related' and go through the first 10 hits and any related link
22:23 PM
then come back
22:23 PM
kalimerisalx
Quick question: is there a way for AuthenticationForm to raise custom validation error or should i Subclass and override clean()
22:23 PM
MikiSoft
i understand how to go with prefetch_related way
22:23 PM
airstrike
you're not just going to have a solution fall from the sky
22:24 PM
MikiSoft
but i don't understand the first one
22:26 PM
cnk
Try it. You will know more after you have played with it. Just make a branch and drop and recreate your dev database
22:27 PM
hutch34 has quit
22:28 PM
brizz has quit
22:29 PM
hamub has quit
22:29 PM
MikiSoft
so if i use this query: Like.objects.filter(person=User.objects.first()).prefetch_related('content_object')
22:30 PM
then i have to loop through all of the objects i get and pull from there events?
22:30 PM
jessamynsmith has quit
22:30 PM
airstrike
do you want to get likes or do you want to get events?
22:30 PM
MikiSoft
events
22:31 PM
airstrike
then it should be Event.objects... something
22:31 PM
MikiSoft
where certain person gave like
22:31 PM
i thought the same and it worked when i still haven't implemented generic relations
22:31 PM
renlo has quit
22:32 PM
Events.objects.filter(like__person=User.objects.first())
22:32 PM
it was plain simple
22:32 PM
*Event.objects...
22:32 PM
and now i'm stuck
22:32 PM
since it doesn't work anymore
22:34 PM
ubuntu_aze has quit
22:34 PM
airstrike
1. read more about GenericForeignKey and how it works
22:34 PM
2. read more about prefetch_related()
22:34 PM
3. google both terms together
22:35 PM
that's all you need to know from us
22:38 PM
renlo joined the channel
22:38 PM
wldcordeiro joined the channel
22:39 PM
ADubhlaoich joined the channel
22:40 PM
cnk
22:40 PM
and I am done
22:40 PM
pyface has quit
22:41 PM
MikiSoft
thank you very much cnk
22:42 PM
Haudegen has quit
22:47 PM
kalimerisalx has quit
22:48 PM
ytsejam has quit
22:48 PM
raijin has quit
22:49 PM
CAPITANOOO joined the channel
22:49 PM
OtherAllan joined the channel
22:52 PM
CAPITANOOO has quit
22:55 PM
22:56 PM
and when i go like this
22:56 PM
for obj in generic_filter(1, Event, 'like'):
22:56 PM
royendgel joined the channel
22:56 PM
print(obj)
22:56 PM
GeorgeJipa has quit
22:57 PM
it throws an exception: django.db.utils.OperationalError: no such column: content_type
22:57 PM
deltaskelta joined the channel
22:58 PM
hutch34 joined the channel
22:58 PM
cnk
This is really not OK as it stands because you are using string interpolation with data that may come from users
22:58 PM
GeorgeJipa joined the channel