When adding tags through the console produces this error (it is in the title) models.py

from django.db import models from django.shortcuts import reverse class Post(models.Model): title = models.CharField(max_length=150, db_index=True) slug = models.SlugField(max_length=150, unique=True) body = models.TextField(blank=True, db_index=True) tags = models.ManyToManyField('Tag', blank=True, related_name='posts') date_pub = models.DateTimeField(auto_now_add=True) def get_absolute_url(self): return reverse('post_detail_url', kwargs={'slug': self.slug}) def __str__(self): return '{}'.format(self.title) class Tag(models.Model): title = models.CharField(max_length=150, db_index=True) slug = models.SlugField(max_length=50, unique=True) def __str__(self): return '{}'.format(self.title) # Create your models here 

. I enter this into the console

 from blog.models import * django_t = (title='django', slug='django') Post.tags Post.tags.add(django_t) 'ManyToManyDescriptor' object has no attribute 'add' 
  • You enter into the console absolutely meaningless sequences of characters. What are you trying to achieve? - Sergey Gornostaev
  • I need to add tags to articles, what am I doing wrong? - Anton Elesin

1 answer 1

Create a new article

 post = Post.objects.create(title='Test', slug='test', body='Lorem ipsum...') 

or get some from the base

 post = Post.objects.first() 

Create a tag

 tag = Tag.objects.create(title='django', slug='django') 

And only then add a tag to the article.

 post.tags.add(tag) 

The relevant section of the documentation . However, I suspect that you should read the entire manual in its entirety.