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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:33:38+00:00 2026-06-11T05:33:38+00:00

What I want to do is to set default values for a m2m field

  • 0

What I want to do is to set default values for a m2m field and I do it in a post_save signal. Here’s minimal code:

# models.py
class Question(models.Model):
    options = models.ManyToManyField(Option)
    body = models.CharField(max_length=140)

def default_options(sender, instance, created, **kwargs):
    if created and not instance.options.all():
        options = Option.objects.filter(id__in=[1, 2])
        instance.options.add(*options) 
        instance.save()

post_save.connect(default_options, sender=Question)

It works fine when “ordinary” saves are called:

>>> q=Question(body='test')
>>> q.save()
>>> q.options.all()
[<Option[1]>, <Option[2]>]

However, if the models are hooked with tastypie, options are never set..

# api.py 
class QuestionResource(ModelResource):
    options = fields.OneToManyField('qa.api.OptionResource', 'options', full=True, blank=True)
    class Meta:
        queryset = Question.objects.all()

# try to create a question:
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body":"test"}' http://localhost:8000/api/0.1/question/

The server would respond a 201 but options of the question is not set.

My questions are:

  • Am I right to use post-save signals to set default values for a m2m field?
  • If so, what’s the hack with tastypie?
  • If not, what’s the right way to do it?
  • I noticed that the tastypie ManyToMany field has a default option. How to use it in this case or where can I find thorough documentation about it..
  • 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-11T05:33:39+00:00Added an answer on June 11, 2026 at 5:33 am

    There are two possible way to handle the m2m relation at django-tastypie side.

    One to to override the obj_create function. Here is for more help.

    class QuestionResource(ModelResource):
       options = fields.OneToManyField('qa.api.OptionResource', 'options', full=True, blank=True)
       class Meta:
          queryset = Question.objects.all()
    
       def obj_create(self, bundle, request, **kwargs):
           print "hey we're in object create"
           # do something with bundle.data,
           return super(QuestionResource, self).obj_create(bundle, request, **kwargs)
    

    And second way is to do it through curl request.

    curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body":"test", "options": ["/api/v1/option/1/"]}' http://localhost:8000/api/0.1/question/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to set the default value in a sql server 2005 datetime field
If I create a class that extends UserControl and want to set a default
I want to be able to set default values for some fields in my
How to set default boolean values with ServletContextPropertyPlaceholderConfigurer ? For example, I want property
I want to set a default value if the variable is not set. Here
I want to set a var default as a random number between two values
Is it possible to set default values for models ? For example consider this
I have an abstract class that I use to set default values in a
I want to set some default values in my wordpress theme options, so that
I want to set my GtkComboBox to have some default value/name, on it as

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.