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

  • Home
  • SEARCH
  • 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 4532142
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:59:32+00:00 2026-05-21T13:59:32+00:00

I have few similar models in Django: class Material(models.Model): title = models.CharField(max_length=255) class Meta:

  • 0

I have few similar models in Django:

class Material(models.Model):
    title = models.CharField(max_length=255)
    class Meta:
        abstract = True

class News(Material):
    state = models.PositiveSmallIntegerField(choices=NEWS_STATE_CHOICES)

class Article(Material):
    genre = models.ForeignKey(Genre, verbose_name='genre')

And model Topic, which is related to News and Article as ManyToMany.

I’d like to use Generic many-to-many relationships like in this case. But question is how to use default ManyToMany widget in django admin. Or another convenient analogue.

UPD: If I didn’t use generics I’d write

class News(Material): 
    topic = models.ManyToMany(Topic) 

class Article(Material):
    topic = models.ManyToMany(Topic)

And I’d get 2 identical tables that express these relationships. I wonder if I could use generics in order to have one intermediate table, because not only news and articles may have topic in my database. News and articles may be connected with 2 or more topics as well.

  • 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-05-21T13:59:32+00:00Added an answer on May 21, 2026 at 1:59 pm

    EDIT: Check this out http://charlesleifer.com/blog/connecting-anything-to-anything-with-django/

    GenericForeignKey’s are unfortunately not as well supported as ForeignKey’s. There’s an open (and accepted) ticket with patch for providing a widget for them: http://code.djangoproject.com/ticket/9976

    What is provided out-of-the-box is managing objects with GenericForeignKey inline.

    Assuming your generic relationship is achieved by

    from django.contrib.contenttypes import generic
    from django.contrib.contenttypes.models import ContentType
    from django.db import models
    
    class News(models.Model):
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey('content_type', 'object_id')
        ...
    

    and

    class Topic(models.Model):
        ...
        news = generic.GenericRelation('News')   # if separate app: 'newsapp.News'
    

    If you want to edit the News of a Topic, you can define an inline admin for News:

    from django.contrib.contenttypes.generic import GenericTabularInline
    
    class NewsInline(GenericTabularInline):
        model = News
    

    and add it to the inlines of Topic admin:

    class TopicAdmin(models.ModelAdmin):
        inlines = (NewsInline, )
    

    That said, from the information given I can’t see what’s wrong with your ManyToMany relationship. It seems to express what you need.

    Maybe you’re defining the ManyToMany field in Topic instead of in News and Article? Define them in News and Article.

    EDIT: Thanks for the clarification. Your model setup would be as per arie’s post (i.e., the other way around) and you’d be editing inline. If you just want to select existing Topic’s from inside a News/Article/etc. instance, I’m not aware of anything out-of-the-box for GenericRelation (which usually just serves as a reverse-lookup helper). You could

    a) Override the admin form and add a ModelMultipleChoiceField with the queryset as per the GenericRelation

    b) Override save() to adjust the relations.

    Quite a lot of work. I would personally stick with multiple m2m tables and not cram everything into one. If you are afraid of the database doing multiple lookups when you ask for all News and Articles and etc. of one or more Topic’s, then be aware that a generic solution will always have a similar setup to the requirements GenericForeignKey has, i.e. additional columns for model and id. That could lead to a lot more queries (e.g. against content_type for each result).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has a few similar controls and the parent contains
I have a few tables that have similar fields but not exactly the same.
I have seen a few other similar questions, but I can't seem to get
I have few asynchronous tasks running and I need to wait until at least
I have few different applications among which I'd like to share a C# enum.
I'm a newbie to pgsql. I have few questionss on it: 1) I know
How do I create subdomains in ASP.NET? I have few links on my homepage(Home.aspx)
We have a few very large Excel workbooks (dozens of tabs, over a MB
I have a few Visual Studio Solutions/Projects that are being worked on in my
I have a few C# .dll projects which are common to many applications. Currently,

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.