1

I am new to Django and currently trying to improve on a design I have been using. This is my models.py file.

from djongo import models
import uuid

PROPERTY_CLASSES = (
    ("p1", "Video Property"),
    ("p2", "Page Property"),
    ("trait", "Context Trait"),
    ("custom", "Custom Property")
)

class Device(models.Model):
    _id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=255)

class Platform(models.Model):
    _id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=255)

class EventPlatformDevice(models.Model):
    _id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = "Event Specifics"
    event_field = models.ForeignKey('Event', on_delete=models.CASCADE)
    applicable_apps = models.ForeignKey('Platform', on_delete=models.CASCADE)
    applicable_devices = models.ManyToManyField('Device')
    property_group = models.ManyToManyField('PropertyGroup', blank=True)
    custom_properties = models.ManyToManyField('Property', blank=True)

class Event(models.Model):
    _id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=255, unique=True)
    applicable_platforms = models.ManyToManyField(Platform, through=EventPlatformDevice)

class PropertyGroup(models.Model):
    _id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=100)
    applicable_properties = models.ManyToManyField('Property')

class Property(models.Model):
    _id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=255)
    #applicable_platform = models.ManyToManyField(Platform, through=PlatformDevice)   
    property_class = models.CharField(max_length=20, choices=PROPERTY_CLASSES)
    format_example = models.TextField(blank=True)
    notes = models.TextField(blank=True)

The issue I am running into is with the EventPlatformDevice model. Right now in the Admin Panel I am able to select multiple items for the applicable_devices, property_group, and custom_properties field. The applicable_apps field is a single select. I am trying to turn that into a multi select as well but when changing the ForeignKey to a ManyToManyField it gives me the error:

<class 'parsers.admin.EventPlatformDeviceInline'>: (admin.E202) 'parsers.EventPlatformDevice' has no ForeignKey to 'parsers.Event'.
parsers.EventPlatformDevice: (fields.E336) The model is used as an intermediate model by 'parsers.Event.applicable_platforms', but it does not have a foreign key to 'Event' or 'Platform'.
Quick33
  • 37
  • 1
  • 5
  • You have to do this with multiple migrations/custom migrations. See https://stackoverflow.com/a/34418830/4151233 – Marco Dec 21 '21 at 15:42

0 Answers0