( ) , db save(). , , 2 . Tag_Relation(source=source, target=target, ...) Tag_Relation(source=target, target=source, ...) :
class Tag_Relation(models.Model):
source = models.ForeignKey(Food_Tag, related_name='source_set')
target = models.ForeignKey(Food_Tag, related_name='target_set')
is_a = models.BooleanField(default=False);
has_a = models.BooleanField(default=False);
class Meta:
unique_together = ('source', 'target')
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
reverse = Tag_Relation.objects.filter(source=self.target, target=self.source)
if reverse.exists():
reverse.update(is_a=self.is_a, has_a=self.has_a)
else:
Tag_Relation.objects.bulk_create([
Tag_Relation(source=self.target, target=self.source, is_a=self.is_a, has_a=self.has_a)
])
The only drawback of this implementation is duplicate entries Tag_Relation, but apart from that, everything works fine, you can even use Tag_Relation in InlineAdmin.
UPDATE
Remember to also define a method deletethat will remove the feedback.
MrKsn source
share