From 5d8ba99361c66305a37ddae7d9b6c4a4061be76f Mon Sep 17 00:00:00 2001 From: Zach Taylor Date: Wed, 21 Oct 2020 16:03:20 -0500 Subject: [PATCH] Change foreign key to many to many to allow more flexibility --- eav/migrations/0001_initial.py | 2 +- eav/models.py | 16 +++++----------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/eav/migrations/0001_initial.py b/eav/migrations/0001_initial.py index 46975bc..fe01334 100644 --- a/eav/migrations/0001_initial.py +++ b/eav/migrations/0001_initial.py @@ -27,7 +27,7 @@ class Migration(migrations.Migration): ('modified', models.DateTimeField(auto_now=True, verbose_name='Modified')), ('required', models.BooleanField(default=False, verbose_name='Required')), ('display_order', models.PositiveIntegerField(default=1, verbose_name='Display order')), - ('entity_ct', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='attribute_entities', to='contenttypes.ContentType')), + ('entity_ct', models.ManyToManyField(to='contenttypes.ContentType', blank=True)), ], options={ 'ordering': ['name'], diff --git a/eav/models.py b/eav/models.py index e25e6a2..7f1f01b 100644 --- a/eav/models.py +++ b/eav/models.py @@ -184,18 +184,12 @@ class Attribute(models.Model): """ required = models.BooleanField(verbose_name = _('Required'), default = False) - entity_ct = models.ForeignKey( - ContentType, - null = True, - blank = True, - on_delete = models.PROTECT, - related_name = 'attribute_entities' - ) + entity_ct = models.ManyToManyField(ContentType, blank=True) """ - This field allows you to specify a foreign key to a content type. - This would be useful, for example, if you wanted an attribute to apply only to one entity. - In that case, you could filter by content type in the :meth:`~eav.registry.EavConfig.get_attributes` - method of that entity's config. + This field allows you to specify a relationship with any number of content types. + This would be useful, for example, if you wanted an attribute to apply only to + a subset of entities. In that case, you could filter by content type in the + :meth:`~eav.registry.EavConfig.get_attributes` method of that entity's config. """ enum_group = models.ForeignKey(