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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:08:15+00:00 2026-05-11T14:08:15+00:00

class Person(models.Model): name = models.CharField(max_length=100) class Entry(models.Model): text = models.CharField(max_length=100) person = models.ManyToManyField(Person, blank=True,

  • 0
class Person(models.Model):     name = models.CharField(max_length=100)  class Entry(models.Model):     text = models.CharField(max_length=100)     person = models.ManyToManyField(Person, blank=True, null=True)  class MyModelForm(forms.ModelForm):     class Meta:         model = Entry 

In my view, I need to add pk id’s to a submitted form before saving it.

data = request.POST.copy() # 'person' is a ManyToManyField to a 'Person' model # a form would normally send multiple id's as POST in this format -> u'id': [u'1', u'2'] # u'1,2' (an example) is a str variable accessible to the view data[u'person'] = u'1,2'.split(',')  form = MyModelForm(data) if form.is_valid():     form.save() 

This gives me:

int() argument must be a string or a number, not ‘list’

Which is fair enough. It does work in case of:

data[u'person'] = u'1' 

I also tried this with no success:

new_form = form.save(commit=False) new_form.person = u'1,2'.split(',') new_form.save() form.save_m2m() 

How can I save multiple id’s to this ManyToManyField?
Must be easy but I am missing the point.


EDIT: The desired result is one new instance of MyModelForm (in the ‘entry’ table) with all id’s stored for form.person (multiple records in the ‘entry_person’ table).


UPDATE: I seem to have isolated the problem.

If I do:

data = {}   data[u'person'] = u'1,2'.split(',') 

It does work as the result is:

{u'person': [u'1', u'2'],}   

If I do:

data = request.POST.copy()   data[u'person'] = u'1,2'.split(',') 

It does NOT work (gives the error described above) as the result is:

<QueryDict: {u'person': [[u'1', u'2']],}> 

So all I need is to have

<QueryDict: {u'person': [u'1', u'2'],}> 

Any suggestions how?

  • 1 1 Answer
  • 3 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. 2026-05-11T14:08:16+00:00Added an answer on May 11, 2026 at 2:08 pm

    QueryDict.setlist(key, list_) solves this problem.

    Answered in A workaround for Django QueryDict wrapping values in lists?

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

Sidebar

Related Questions

Here is a Django model I'm using. class Person(models.Model): surname = models.CharField(max_length=255, null=True, blank=True)
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have these models: class Person(models.Model): name=models.CharField(max_length=100) family=models.CharField(max_length=100) class MailContact(models.Model): person=models.ForeignKey(Person) email=models.CharField(max_length=100) #some fields
I defined two models: class Server(models.Model): owners = models.ManyToManyField('Person') class Person(models.Model): name = models.CharField(max_length=50)
Imagine I have the following model: class Person(models.Model): ...other stuff... optional_first_name= models.CharField(max_length=50, blank=True) How
Here's part of my Django app's models.py : class Person(models.Model): birth_year = WideYear(null=True, blank=True)
I have a symmetrical many-to-many relationship in Django class Person(models.Model): id = models.CharField(max_length=32, primary_key=True)
I have this models in Django class Country(models.Model): name = models.CharField(max_length=80) class Person(models.Model): first_name
Hay, I have a Model which looks like this class Person(models.Model): name = models.CharField(blank=False,
I have three classes Class Company(models.Model): name = CharField( max_length = 26 , blank

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.