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 7012143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:13:03+00:00 2026-05-27T22:13:03+00:00

I have the following models: class Message(models.Model): date = models.DateTimeField() user = models.ForeignKey(User) thread

  • 0

I have the following models:

class Message(models.Model):
    date = models.DateTimeField()
    user = models.ForeignKey(User)    
    thread = models.ForeignKey('self', blank=True, null=True)
    ...

class Forum(models.Model):
    name = models.CharField(max_length=24)
    messages = models.ManyToManyField(Message, through="Message_forum", blank=True, null=True)
    ...

class Message_forum(models.Model):
    message = models.ForeignKey(Message)
    forum = models.ForeignKey(Forum)
    status = models.IntegerField()
    position = models.IntegerField(blank=True, null=True)
    tags = models.ManyToManyField(Tag, blank=True, null=True)

In the admin site, when I go to add/change a forum, I don’t see the messages listbox as you’d expect. However, it shows up if I remove the ‘through’ parameter in the ManyToManyField declaration. What’s up with that? I’ve registered all three models (plus Tag) to the admin site in admin.py.

TIA

  • 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-27T22:13:04+00:00Added an answer on May 27, 2026 at 10:13 pm

    Documentation says:

    When you specify an intermediary model using the through argument to a
    ManyToManyField, the admin will not display a widget by default.

    But it’s probably possible to display M2M fields in the admin change view even if the through attribute is defined.

    class ForumAdminForm(forms.ModelForm):
        mm = forms.ModelMultipleChoiceField(
            queryset=models.Message.objects.all(),
            widget=FilteredSelectMultiple(_('ss'), False, attrs={'rows':'10'}))
    
        def __init__(self, *args, **kwargs):
            if 'instance' in kwargs:
                initial = kwargs.setdefault('initial', {})
                initial['mm'] = [t.service.pk for t in kwargs['instance'].message_forum_set.all()]
    
            forms.ModelForm.__init__(self, *args, **kwargs)
    
        def save(self, commit=True):
            instance = forms.ModelForm.save(self, commit)
    
            old_save_m2m = self.save_m2m
            def save_m2m():
                old_save_m2m()
    
                messages = [s for s in self.cleaned_data['ss']]
                for mf in instance.message_forum_set.all():
                    if mf.service not in messages:
                        mf.delete()
                    else:
                        messages.remove(mf.service)
    
                for message in messages:
                    Message_forum.objects.create(message=message, forum=instance)
    
            self.save_m2m = save_m2m
    
            return instance
    
        class Meta:
            model = models.Forum
    
    class ForumAdmin(admin.ModelAdmin):
        form = ForumAdminForm
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following is the models.py: class UserProfile(models.Model): user = models.OneToOneField(User) belongs_to_user_category = models.ForeignKey(UserCustomCategory, null=True, blank=True)
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
Say I have the following in my models.py : class Company(models.Model): name = ...
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have a Rails 2.3 app with the following models. class Message << AR::Base

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.