1from django.db import models
2from django.utils.encoding import python_2_unicode_compatible
3
4from cms.models import CMSPlugin
5
6
7@python_2_unicode_compatible
8class Article(models.Model):
9    title = models.CharField(max_length=50)
10    section = models.ForeignKey('Section', on_delete=models.CASCADE)
11
12    def __str__(self):
13        return u"%s -- %s" % (self.title, self.section)
14
15
16@python_2_unicode_compatible
17class Section(models.Model):
18    name = models.CharField(max_length=50)
19
20    def __str__(self):
21        return self.name
22
23
24@python_2_unicode_compatible
25class ArticlePluginModel(CMSPlugin):
26    title = models.CharField(max_length=50)
27    sections = models.ManyToManyField('Section')
28
29    def __str__(self):
30        return self.title
31
32    def copy_relations(self, oldinstance):
33        self.sections.set(oldinstance.sections.all())
34
35
36###
37
38
39class FKModel(models.Model):
40    fk_field = models.ForeignKey('PluginModelWithFKFromModel', on_delete=models.CASCADE)
41
42
43class M2MTargetModel(models.Model):
44    title = models.CharField(max_length=50)
45
46
47class PluginModelWithFKFromModel(CMSPlugin):
48    title = models.CharField(max_length=50)
49
50    def copy_relations(self, oldinstance):
51        # Like suggested in the docs
52        for associated_item in oldinstance.fkmodel_set.all():
53            associated_item.pk = None
54            associated_item.fk_field = self
55            associated_item.save()
56
57
58class PluginModelWithM2MToModel(CMSPlugin):
59    m2m_field = models.ManyToManyField(M2MTargetModel)
60
61    def copy_relations(self, oldinstance):
62        # Like suggested in the docs
63        self.m2m_field.set(oldinstance.m2m_field.all())
64