You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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??
The text was updated successfully, but these errors were encountered:
@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?
I am using djagno-taggit. I want to add other fields data automatically when I save the data table.
models.py:
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??
The text was updated successfully, but these errors were encountered: