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

django taggit self.tags.add() in not working in django #819

Open
naveedur opened this issue Aug 20, 2022 · 1 comment
Open

django taggit self.tags.add() in not working in django #819

naveedur opened this issue Aug 20, 2022 · 1 comment

Comments

@naveedur
Copy link

naveedur commented Aug 20, 2022

I am using djagno-taggit. I want to add other fields data automatically when I save the data table.
models.py:

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)
    varient=models.CharField(max_length=100, default="")
    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="", unique=True, blank=True)
    tags_char=models.TextField( blank=True)
    Tags = TaggableManager(blank=True) 

    def save(self, *args, **kwargs):
      getlast=resource.objects.all().last()
      getid=getlast.id
      print(getid)
      newid=int(getid)+1
      if not self.pk:
          self.pk=newid
      self.tags_char= self.desc+","+self.size
      ans=[self.title ,self.desc,self.size]
      self.Tags.add(*ans)
      print(self.Tags.all())
      for tag in self.tags_char.split(','):
              print(tag)
      super(resource, self).save(*args, **kwargs)

Here first I get pk for current data and try to save list that has the same data fields in the Tags fields. It works well when I Print Tagsafter adding and get the below result in my terminal when I

print(self.Tags.all())
result in the terminal is:
<QuerySet [<Tag: tagSize>, <Tag: tagTitle>, <Tag: tagDescription>]>
But when I open it in the admin models data table then I got Tags fields empty!!
Any solution??

@rtpg
Copy link
Contributor

rtpg commented Nov 14, 2022

@naveedur I'm not 100% sure here but your code might be having an issue because you call super().save(*args, **kwargs) after editing the tags (so the pk might get changed up).

Could you try calling super().save(*args, **kwargs), then saving your tags?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants