简体   繁体   中英

Django model that stores multiple other models that store multiple other models

I am learning Django so this is all very new to me. What I am trying to create is a bit of functionality in my admin panel that will allow me to create a layout like this.

Test
-Event1
--Property1
--Property2
--Property3
--Property4
-Event2
--Property1a
--Property2b
--Property3c
--Property4d
-Event3
--Property1aa
--Property2bb
--Property3cc
--Property4dd
-Event4
--Property1aaa
--Property2bbb
--Property3ccc
--Property4ddd

I want to have multiple tests. My current model setup looks like this:

from django.db import models
from django.forms import ModelForm

TYPE_CHOICES = (
    ("string", "string"),
    ("integer", "integer"),
    ("array", "array"),
    ("boolean", "boolean")
)

class Test(models.Model):
    name = models.CharField(max_length=255)
    description = models.CharField(max_length=255, blank=True)

    class Meta:
        verbose_name = 'Test'
        verbose_name_plural = 'Tests'

    def __str__(self):
        return self.name


class Event(models.Model):
    name = models.CharField(max_length=255)
    test_id = models.IntegerField()

    class Meta:
        verbose_name = 'Event'
        verbose_name_plural = 'Events'

    def __str__(self):
        return self.name


class Property(models.Model):
    name = models.CharField(max_length=255)
    property_type = models.CharField(max_length=20, choices=TYPE_CHOICES)
    expected_value = models.CharField(max_length=255)

    class Meta:
        verbose_name = 'Property'
        verbose_name_plural = 'Properties'

    def __str__(self):
        return self.name

class TestForm(ModelForm):
    class Meta:
        model = Test
        fields = ['name', 'description']

I have my admin panel setup so that I can create multiple properties. But then when I go to the "Events" section in my admin panel I can only create events. I want to be able to pick the properties and add them to my event. Then I want to be able to go to the Test page and add the events to it.

A good example of what I am trying to create is a replica of this: http://jsonparser.tools/tests.php

you should define foreign keys for events and properties:

from django.db import models
from django.forms import ModelForm

TYPE_CHOICES = (
    ("string", "string"),
    ("integer", "integer"),
    ("array", "array"),
    ("boolean", "boolean")
)

class Test(models.Model):
    name = models.CharField(max_length=255)
    description = models.CharField(max_length=255, blank=True)

    class Meta:
        verbose_name = 'Test'
        verbose_name_plural = 'Tests'

    def __str__(self):
        return self.name


class Event(models.Model):
    name = models.CharField(max_length=255)
    test = models.ForeignKey(Test,on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Event'
        verbose_name_plural = 'Events'

    def __str__(self):
        return self.name


class Property(models.Model):
    event = models.ForeignKey(Event,on_delete=models.CASCADE)
    name = models.CharField(max_length=255)
    property_type = models.CharField(max_length=20, choices=TYPE_CHOICES)
    expected_value = models.CharField(max_length=255)

    class Meta:
        verbose_name = 'Property'
        verbose_name_plural = 'Properties'

    def __str__(self):
        return self.name

class TestForm(ModelForm):
    class Meta:
        model = Test
        fields = ['name', 'description']

this should solve your problem if not let me know happy to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM