0

I'm trying to access all my different model objects by the Hit table. What I do not understand is why I'm unable to Build the actual query to do that. I simply want to get all objects at queryset_m1-m3 where the Model1-3.pk is the hit.object_id as hit.object_id is the primary key of a related object of Model1, 2 or 3.

def recently_viewed_proposals():
    hit = Hit.objects.all()
    queryset_m1 = Model1.objects.filter(pk=hit.object_id)
    queryset_m2 = Model2.objects.filter(pk=hit.object_id)
    queryset_m3 = Model3.objects.filter(pk=hit.object_id)
    hit_elements = chain(
        queryset_m1.random(len(queryset_m1)),
        queryset_m2.random(len(queryset_m2)),
        queryset_m3.random(len(queryset_m3))
    )
    elements_list = list(hit_elements)
    n_proposals = min(len(elements_list), config.MAX_RECENTLY_VIEWED_PROPOSALS)
    recently_viewed_proposals = random.sample(elements_list, n_proposals)
    return recently_viewed_proposals

This is how my Hit model Class looks like:

...

class Hit(models.Model):
    content_type = models.ForeignKey(ContentType, limit_choices_to=hit_models, on_delete=models.CASCADE)
    object_id = models.CharField(max_length=36)
    content_object = GenericForeignKey('content_type', 'object_id')
    viewer = models.ForeignKey(User, verbose_name="Viewer", on_delete=models.CASCADE)
    counted = models.BooleanField(verbose_name="Counted", default=False, editable=True)
    date_added = models.DateTimeField(auto_now=True)

But I'm always falling into the following issue:

'QuerySet' object has no attribute 'object_id'

Of course my Model(s)1-3 containing the field:

hits = GenericRelation(Hit, related_query_name='hit')

Thanks in advance

1 Answers1

0
hit = Hit.objects.all() 

is returning a queryset (a "django list" of all the hits), so you cannot do the following

queryset_m1 = Model1.objects.filter(pk=hit.object_id)
queryset_m2 = Model2.objects.filter(pk=hit.object_id)
queryset_m3 = Model3.objects.filter(pk=hit.object_id)

Cause object_id is not an attribute of queryset

You can do something like the following and it should work:

queryset_m1 = Model1.objects.filter(pk__in=hit.filter(content_type_id=id_model_1).values_list('id', flat=True))
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Cool, but I don't exactly understand what "content_type_id=id_model_1" should be? Can you explain or at least provide something that fills this placeholder for this scenario? Thanks –  Oct 12 '20 at 14:58
  • Okay I tested your Answer in almost any way also with the bare content_type_id which is 12, 15 and 20 in my case, sadly without success. Actually I would prefer this way: > > =hit.filter(content_type=str(hit.content_type) == 'App | Model1' < < As you never know the content ID and it's also not always the same if you e.g. redeploy your app. But anyway this answer seems wrong to me. The only thing that gets returned is: ... –  Oct 12 '20 at 18:35
  • @sunwave121 What you are getting "" is an iterable, cause you are using chain(), you then need to cast it in your case into a list as you were doing elements_list = list(hit_elements) – Roberto Fernandez Diaz Oct 12 '20 at 22:32
  • Hey, thanks for your reply. What exactly do I need to convert into a list? currently print(recently_viewed_proposals) returns [] ... So it seems to be empty anyways if I do content_type_id=12, 15, 20. Also n_proposals returns 0 –  Oct 13 '20 at 08:17
  • Just to prevent a misunderstanding, I'm trying to query my Hit table for Objects of Model1,2,3 –  Oct 13 '20 at 08:26
  • What do you have on queryset_m1, queryset_m2 and queryset_m3? Does those queries return any element? – Roberto Fernandez Diaz Oct 13 '20 at 11:56
  • Also empty, seems the query's are wrong in general :( –  Oct 13 '20 at 12:54
  • Then you probably mess up something before in the model, but I am afraid I cannot help more, try to review your schema in the database and see if everything is as you expect – Roberto Fernandez Diaz Oct 13 '20 at 14:32
  • Thanks anyways but if the model or the pre-processing is wrong I would be able to target my data and save view counts of my objects. I will update this thread as soon as I have resolved this issue, thanks again! –  Oct 13 '20 at 18:17