Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tags list to tag field by post save method #810

Closed
engrimran7 opened this issue Jul 17, 2022 · 1 comment
Closed

Adding tags list to tag field by post save method #810

engrimran7 opened this issue Jul 17, 2022 · 1 comment

Comments

@engrimran7
Copy link

engrimran7 commented Jul 17, 2022

i am trying to automatically add tags list to the tag field by post save method to copy all the fileld's to tags field.

i tried below which shows success without any exception but the issue is all the fields are added to tags table in admin but not associating the tags to that specific resource tag field.

[class resource(models.Model):
title=models.CharField(max_length=100)
size=models.CharField( max_length=20, default="")
desc=models.TextField(default="")
file=models.FileField(default="", blank=True)
url= models.URLField(max_length=200, blank=True)
Brand = models.ForeignKey(brand,on_delete=models.CASCADE, default="")
Model = models.ForeignKey(model,on_delete=models.CASCADE, default="")
Categories = models.ForeignKey(category,on_delete=models.CASCADE, default="")
update_at=models.DateField(auto_now=True)
slug=models.SlugField(default="")
Tags = TaggableManager()

def save(self, *args, **kwargs):
    if not self.id:
        # Newly created object, so set slug
        self.slug = slugify(self.title)
    super(resource, self).save(*args, **kwargs)

def Tag(self):
    return ",".join([str(p) for p in self.Tags.all()] )
def _str_(self):
 return self.title

def tag_set(sender, instance,*arg, **kwargs):
ans= array_tag(instance)
# print(ans)
mylist = ["firmware", "download", "gsm"]
instance.title='check'
instance.Tags.add(*ans)

post_save.connect(tag_set, sender=resource)
def array_tag(instance):
return [instance.title ,instance.desc,instance.size, instance.Brand.title ,instance.Model.title ,instance.Categories.title]
](url)

i also gave it a try by changing code as below which through below error.

changed this
def tag_set(sender, instance,*arg, **kwargs): ans= array_tag(instance) # print(ans) mylist = ["firmware", "download", "gsm"] instance.title='check' instance.Tags.add(*ans)

to this
def tag_set(sender, instance,*arg, **kwargs): ans= array_tag(instance) # print(ans) mylist = ["firmware", "download", "gsm"] instance.title='check' instance.Tags.add(ans)

and got below error

Cannot add ['tags_check', 'check file', '6G', 'oppo', 'oppoA37', 'china'] (<class 'list'>). Expected <class 'django.db.models.base.ModelBase'> or str.

any help by experts is highly appreciated

@rtpg
Copy link
Contributor

rtpg commented Nov 14, 2022

This seems to be a duplicated of #818

@rtpg rtpg closed this as completed Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants