Answer the question
In order to leave comments, you need to log in
Django - how to add value to ManyToManyField via admin?
Hello. Straight to the point. There are models:
class Tag(models.Model):<br>
name = models.CharField(verbose_name=u'Tag', max_length=200, unique=True)<br><br>
class Entry(models.Model):<br>
title = models.CharField(verbose_name=u'Entry title', max_length=200)<br>
# всякие поля<br>
tags_string = models.CharField(verbose_name=u'Tags', max_length=200, blank=True)<br>
tags = models.ManyToManyField(Tag, null=True, blank=True)<br>
def save(self):<br>
super(Entry, self).save()<br>
if self.tags_string:<br>
for tag in tags_string.split(","):<br>
t = Tag.objects.create(name=tag.lstrip())<br>
self.tags.add(t)<br>
def save(self):<br>
super(Entry, self).save()<br>
self.tags.add(3) # где 3 - id тэга.<br>
Answer the question
In order to leave comments, you need to log in
Your problem is in the admin. Automatic field for tags automatically removes tags.
If it is possible in your situation to remove this field painlessly, I advise you to do it.
Well, help:
class Tag ( models. Model ) :
name = models. CharField ( verbose_name=u 'Tag' , max_length= 200 , unique= True )
def __unicode__ ( self ) :
return self . name
class Entry ( models. Model ) :
title = models. CharField ( verbose_name=u 'Entry title' , max_length= 200 )
tags_string = models. CharField (verbose_name=u 'Tags' , max_length= 200 , blank= True )
# Note editable=False
tags = models. ManyToManyField ( Tag, null= True , blank= True , editable= False )
def __init__ ( self , * args, ** kwargs ) :
super ( Entry, self ) . __init__ ( * args, ** kwargs )
# Initialize ``tags_string`` with tag values
if self . pk :
self . tags_string = ', ' . join ( self . tags . values_list ( 'name' , flat= True ) )
def save ( self , * args, ** kwds ) :
super ( Entry, self ) . save ( * args, ** kwds )
names = set ( [name. strip ( ) for name in self . tags_string . split ( ',' ) ] )
current = set ( self . tags . values_list ( 'name' , flat= True ) )
# Not only add but also remove tags
exclude = current - names
for name in names:
tag, created = Tag. objects . get_or_create ( name=name )
self. tags . add ( tag )
for name in exclude:
tag = Tag. objects . get ( name=name )
self . tags . remove ( tag )
class EntryAdmin ( admin. ModelAdmin ) :
model = Entry
list_display = ( 'title' , 'tag_list' )
def tag_list ( self , obj ) :
return ', ' . join ( obj. tags . values_list ( 'name' , flat= True ) )
admin. site . register ( Entry, EntryAdmin )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question