An empty Django 3.2 project, and only this content in the model.py file:
from django.db import models
name = models.CharField(max_length=150, verbose_name="Tag Name")
name = models.CharField(max_length=150, verbose_name="Post Name")
tags = models.ManyToManyField(Tag, blank=True, related_name='posts')
def save(self, *args, **kwargs):
tags = ['a','b','c','d','e']
for tag in tags:
dbtag, created = Tag.objects.get_or_create(name=tag)
Code creates tags
Code makes relations because print- command prints:
<QuerySet [<Tag: a>, <Tag: b>, <Tag: c>, <Tag: d>, <Tag: e>]>
The tags go to the database.
Relatioat does not end up in the database, Why?
Bug or am I doing something wrong?
When posting code, please enclose the code between lines consisting of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This allows the forum software to keep your code properly formatted, which is essential with Python. (You can also edit your original post to put the ``` before and after the code you originally posted.)
To get to your original question, first, it appears like you’re calling
super().save(...) twice. You don’t need to do a save after adding the related objects through a ManyToMany relationship. See the note at the end of the Related objects reference docs. (You’re not altering the Post model, the data being saved for the ManyToMany goes into the join table created for that relationship.)
Also, what are you looking at that is making you believe that the relations between Post and Tag are not being saved in the database?
When I check a Post in Django admin after the adding it, it has no tags selected.
Also when I look at the contents of db.sqlite3, then the test1_post_tag table is empty.
If that post above is an accurate representation of your code, then your indentation is incorrect.
The fields and functions should be indented as part of their respective classes.
I fixed, the indentation have been correct in the code, but for some reason in this message they were incorrect. Sorry.
I am unable to recreate the behavior you’re reporting here. There’s something else here that’s hiding what’s actually occurring. Seeing that self.tags.all is printing the set of related objects makes me believe that those relations are being created.
Please post the view in which this save is being called. (It’s possible that the transaction is being rolled back after the save method exits due to a subsequent error.)
I noticed an interesting thing, this code works perfectly when using a shell (python manage.py shell) and storing relations in a database. But when you use Django admin, it doesn’t save relations!
Ok, it appears to be a “sequence of events” type of thing. The save for the base model (Post) is looks like it’s being performed before the Admin processes the UI entries for the M2M field. So what’s happening is that the relationships you’re trying to define in the model’s save method are being made - but after that, the admin is taking whatever selections were made in the UI and overriding them. (At least that’s the behavior that I seem to be seeing locally.)
If you change the
tags field to readonly, then the admin won’t override your manually-created entries with the UI selections.
Thanks, this helped. But what if I need tags to be editable? For example, I pass that code to the signals and run only when the post is created (if created:) The code generates tags based on the content of the message, but the tags are later editable by the user. How can I do this?