How to set the value of ManyToMany field in Django?

while learning Django for web programming, I ran into this problem. I searched Google and the official Django site, but could not find an answer. Please help me.

System environment:

  • Fedora 18
  • Python 2.7
  • Django 1.5.1
  • Eclipse + PyDev

Runtime: Django Development Server

I have models with ManyToMany field. When I set the value using django admin panel, everything goes well. The codes below are all my models:

class Posts(models.Model): post_authorid = models.IntegerField(verbose_name=u'Authorid') post_date = models.DateTimeField(auto_now_add=True,verbose_name=u'PostDate') post_date_modified = models.DateTimeField(auto_now=True,verbose_name=u'LastModifiedTime') post_content = models.TextField(verbose_name=u'Content') post_title = models.CharField(max_length=50,verbose_name=u'Title') post_name = models.CharField(max_length=50,blank=True,verbose_name=u'ShortName') post_cover = models.CharField(max_length=200,verbose_name=u'CoverUrl') post_introduction = models.CharField(max_length=500,blank=True,verbose_name=u'introduction') post_status = models.ForeignKey(Status,verbose_name=u'status') comment_status = models.BooleanField(verbose_name=u'show_comments') post_password = models.CharField(max_length=20,blank=True,verbose_name=u'passwd') post_tagid = models.ManyToManyField(Tags,verbose_name=u'tag') post_threadtypeid = models.ForeignKey(ThreadTypes,verbose_name=u'ThreadType') post_comment_conut = models.IntegerField(verbose_name=u'CommentsCount') post_comments = models.ManyToManyField(Comments,blank=True,verbose_name=u'Comment') def __unicode__(self): return u"%s %s %s" % (self.id,self.post_title,self.post_date) class Meta: ordering = ['post_date'] class Tags(models.Model): tagname = models.CharField(max_length=20,verbose_name=u'标签名称') def __unicode__(self): return u"%s %s" % (self.id,self.tagname) class Meta: ordering = ['id'] 

In my python shell, I type:

 post = Posts() post.post_tagid = Tags.objects.get(id='1') 

then django rasize http 500 error:

An object

must be relevant for field messages before many-to-many relationships can be used.

But, when I use:

 post= Posts.objects.get(id='1') 

Note. I entered a Posts object by Django admin

Then when i use

 post.post_tagid = Tags.objects.get(id='1') 

everything goes well.

Q: How can I add a ManyToMany field without raising this error?

+4
source share
2 answers

It seems like the problem is that you are trying to add something to the M2M table before it is created in the database.

When you run post = Posts() you create an object in memory, but not in the database. Therefore, when you try to add a new record to the M2M table, there is nothing to refer to. (Remember that declaring an M2M field creates a new table with records pointing to both ends of the relationship.)

The solution is to run post.save() before trying to add M2M to the table. (The Django admin does this for you behind the scenes.)

So try something like this:

 post = Posts() # now set up the post (title, name, etc.) post.save() # adds a new entry to the Posts table post.post_tagid.add(tag) # now that your post exists in the DB this should work 
+12
source

You should read django's many-to-many white papers.

In your case, the following code should help:

 # Post object post= Posts.objects.get(id=1) # Two tag objects tag1 = Tags.objects.get(id=1) tag2 = Tags.objects.get(id=2) # Add tag objects to Post object post.post_tagid.add(tag1, tag2) 
0
source

Source: https://habr.com/ru/post/1493118/


All Articles