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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:26:42+00:00 2026-06-02T02:26:42+00:00

This is how my models look: class QuestionTagM2M(models.Model): tag = models.ForeignKey(‘Tag’) question = models.ForeignKey(‘Question’)

  • 0

This is how my models look:

class QuestionTagM2M(models.Model):
    tag = models.ForeignKey('Tag')
    question = models.ForeignKey('Question')
    date_added = models.DateTimeField(auto_now_add=True)

class Tag(models.Model):
    description = models.CharField(max_length=100, unique=True)

class Question(models.Model):
    tags = models.ManyToManyField(Tag, through=QuestionTagM2M, related_name='questions')

All I really wanted to do was add a timestamp when a given manytomany relationship was created. It makes sense, but it also adds a bit of complexity. Apart from removing the .add() functionality [despite the fact that the only field I’m really adding is auto-created so it technically shouldn’t interfere with this anymore]. But I can live with that, as I don’t mind doing the extra QuestionTagM2M.objects.create(question=,tag=) instead if it means gaining the additional timestamp functionality.

My issue is I really would love to be able to preserve my filter_horizontal javascript widget in the admin. I know the docs say I can use an inline instead, but this is just too unwieldy because there are no additional fields that would actually be in the inline apart from the foreign key to the Tag anyway.

Also, in the larger scheme of my database schema, my Question objects are already displayed as an inline on my admin page, and since Django doesn’t support nested inlines in the admin [yet], I have no way of selecting tags for a given question.

Is there any way to override formfield_for_manytomany(self, db_field, request=None, **kwargs) or something similar to allow for my usage of the nifty filter_horizontal widget and the auto creation of the date_added column to the database?

This seems like something that django should be able to do natively as long as you specify that all columns in the intermediate are automatically created (other than the foreign keys) perhaps with auto_created=True? or something of the like

  • 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-02T02:26:45+00:00Added an answer on June 2, 2026 at 2:26 am

    There are ways to do this

    • As provided by @obsoleter in the comment below : set QuestionTagM2M._meta.auto_created = True and deal w/ syncdb matters.
    • Dynamically add date_added field to the M2M model of Question model in models.py

      class Question(models.Model):
          # use auto-created M2M model
          tags = models.ManyToMany(Tag, related_name='questions')
      
      
      # add date_added field to the M2M model
      models.DateTimeField(auto_now_add=True).contribute_to_class(
               Question.tags.through, 'date_added')
      

      Then you could use it in admin as normal ManyToManyField.
      In Python shell, use Question.tags.through to refer the M2M model.

      Note, If you don’t use South, then syncdb is enough; If you do, South does not like
      this way and will not freeze date_added field, you need to manually write migration to add/remove the corresponding column.

    • Customize ModelAdmin:

      1. Don’t define fields inside customized ModelAdmin, only define filter_horizontal. This will bypass the field validation mentioned in Irfan’s answer.
      2. Customize formfield_for_dbfield() or formfield_for_manytomany() to make Django admin to use widgets.FilteredSelectMultiple for the tags field.
      3. Customize save_related() method inside your ModelAdmin class, like
    def save_related(self, request, form, *args, **kwargs):
        tags = form.cleaned_data.pop('tags', ())
        question = form.instance
        for tag in tags:
            QuestionTagM2M.objects.create(tag=tag, question=question)
        super(QuestionAdmin, self).save_related(request, form, *args, **kwargs)
    
    • Also, you could patch __set__() of the ReverseManyRelatedObjectsDescriptor field descriptor of ManyToManyField for date_added to save M2M instance w/o raise exception.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I've got a set of models that look like this: class Page(models.Model): title =
I have a django model and model form that look like this: -models.py class
So I have a few Django models that look like this: class City(models.Model): name
In Django I have this: models.py class Book(models.Model): isbn = models.CharField(max_length=16, db_index=True) title =
My models look like this: class Category(models.Model): name = models.CharField(_('name'), max_length=50) class Product(models.Model): name
This is what part of my models look like: class Character(models.Model): name = models.CharFied()
I have 2 models which look like that: class Entry(models.Model): user = models.ForeignKey(User) dataname
My models look like this: class Article(models.Model): name = models.CharField(max_length=50) description = models.TextField() price
I have models that look like this (simplified of course): class Product(models.Model): product_code =

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.