I have a Django model Article
and after saving an instance, I want to find the five most common words (seedwords) of that article and save the article in a different model Group
, based on the seedwords.
The problem is that I want the Group
to be dependent on the instances of Article
, i.e. every time an Article
is saved, I want Django to automatically check all existing groups and if there is no good fit, create a new Group
.
class Article(models.Model):
seedword_group = models.ForeignKey("Group", null=True, blank=True)
def update_seedword_group(self):
objects = News.objects.all()
seedword_group = *some_other_function*
return seedword_group
def save(self, *args, **kwargs):
self.update_seedword_group()
super().save(*args, **kwargs)
class Group(models.Model):
*some_fields*
I have tried with signals but couldn't work it out and I'm starting to think I might have misunderstood something basic.
So, to paraphrase:
- I want to save an instance of a model A.
- Upon saving, I want to create or update an existing model B depending on A via ForeignKey.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…