Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:36:13+00:00 2026-06-14T21:36:13+00:00

I made a generic foreign key to be able to reference any type (A

  • 0

I made a generic foreign key to be able to reference any type (A or B) of model in the Report model in a generic way.
It works fine but my problem is that now, I need to make a query to retrieve all Report instance referencing a specific type of Model (e.g. all Report with a foreign_key referencing class A). I tried to access the content_type in the database but apparently it is not possible (the variable does not exist…)

Here are my models:

class Report(models.Model):
  content_type = models.ForeignKey(ContentType)
  object_id = models.CharField(max_length=50)
  reported_item = generic.GenericForeignKey('content_type', 'object_id')

class A(models.Model):
  reports = generic.GenericRelation(Report)

class B(models.Model):
  reports = generic.GenericRelation(Report)

Do you have any idea how to do that?
Just in case it would change anything, I use mongoDB for my database.
Thanks very much!

EDIT:
When I display the report object in the db, here is the result:

db.website_report.findOne()
{
"_id" : ObjectId("50afa903a96c5c2f63000001"),
"content_type_id" : ObjectId("50afa903a96c5c2f63000000"),
"object_id" : "50afa8a8a96c5c2f53000001",
}

As you can see, the content of the database has nothing similar with my models so I don’t know how to do a query to get all the report having a foreign_key of A.
Can you help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:36:15+00:00Added an answer on June 14, 2026 at 9:36 pm

    I don’t understand why you think the db does not reflect your model. There you have the content type reference, and the object id. All you need to vdo is find out what content type refers to your model A, which is explained in the contenttype documentation:

    ct = ContentType.objects.get_for_model(A)
    objs = Report.objects.filter(content_type=ct)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've built this model which contains a generic foreign key: class MyModel(models.Model): content_type =
What made me curious is that the generic handler works just fine when I'm
I have made a generic Observer interface and an Observable class, but can't compile
I made in my web a menu using generic_view - simple 'django.views.generic.list_detail.object_list' in urls.py
Made this nice little loop for hiding and showing div's, works as a charm
I made a dll in c# but i need to export the functions. Is
I've made a generic extension method that executes an action on an object and
Recently I am having many problem with typedef and incomplete type when I changed
I want to create a generic function that takes any Map & a String
I have made a generic handler (.ashx) to fetch some database values using jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.