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

tags.add function not working in pre-save/post-save in django #818

Open
naveedur opened this issue Aug 18, 2022 · 9 comments
Open

tags.add function not working in pre-save/post-save in django #818

naveedur opened this issue Aug 18, 2022 · 9 comments

Comments

@naveedur
Copy link

naveedur commented Aug 18, 2022

I am using django taggit for tags. I want to add fields text in tags for some data table. But its not working.
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 = TaggableManager(blank=True)

pre-save function:

def tag_set(sender, instance, **kwargs):

    ans=[instance.title ,instance.Brand,instance.Model]
    instance.tags.add(*ans)

pre_save.connect(tag_set, sender=resource)

It's neither showing any error nor working. Any one please help if any solution or alternative.

@rtpg
Copy link
Contributor

rtpg commented Aug 18, 2022

Does this code not work if you use the post_save hook instead of the pre_save hook? pre_save will likely run into issues with the instance changing, but post_save should probably work.

@naveedur
Copy link
Author

@rtpg No it's also not working for post-save

@rtpg
Copy link
Contributor

rtpg commented Aug 18, 2022

Could you do something like:

print(instance.tags.all())
print(ans)
instance.tags.add(*ans)
print(instance.tags.all())

and show the results here? that might make it a bit easier to debug what is going on

@naveedur
Copy link
Author

naveedur commented Aug 18, 2022

after that I got this ;

<QuerySet []> ['tgas-title', 'description', '4G'] <QuerySet [<Tag: description>, <Tag: 4G>, <Tag: tgas-title>]>

But tags are not saved in data table tags field.

@naveedur
Copy link
Author

@rtpg please any suggestion for that

@chambersh1129
Copy link
Member

I made some progress on tracking this down but not entirely sure how to address it. Looks like when creating/editing an object in the admin view with a post_save signal the order of operations is:

  • model.save() function is run
  • post_save signal is fired off, tags are updated
  • TaggableManager.save_form_data() is run, which runs the _TaggableManager.set() function, replacing the post_save tags with what is in the text field when the admin view form was submitted.
  • since model.save() isn't run again, the post_save doesn't run a second time

Looks like the tag is added with the post_save then overwritten with the .set() in the save_form_data. I can work on a PR to try to address this, thoughts on the best method?

@chambersh1129
Copy link
Member

Made a little more progress identifying the issue:

This is why printing the tags before and after the add in the post_save shows the correct tags, but once saved the post-save tags are gone. The solution to form.save(commit=False) is listed in the docs. I've played around with it a little, attempting to overwrite various save methods and setting custom forms for the Admin view but everything I've tried brings other problems.

I'm open to ideas. I'll keep investigating as I have time.

@rtpg
Copy link
Contributor

rtpg commented Nov 14, 2022

@chambersh1129 thanks for digging into this, I get what you're saying.

I don't think this is really a bug, signals are really nastly like this. For me what I do in these situations is to just give in and write a update_default_tags function that I call manually in an admin form subclass or the like. Ultimately in a form save, if we get a list of tags, calling set instead of add is the least bugful thing to do here.

So yeah, recommendation here is to overwrite the Admin form (there's a save_related method) or otherwise do stuff to confirm that these tags are present/in-sync.

@rtpg rtpg closed this as completed Nov 14, 2022
@rtpg rtpg reopened this Nov 14, 2022
@rtpg rtpg added the bug label Nov 14, 2022
@rtpg
Copy link
Contributor

rtpg commented Nov 14, 2022

I'm labelling this a bug but it's not really a bug IMO. Just thinking about what the right way forward can be for this kind of thing

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

3 participants