Reputation: 25
I'm newbie in Django and making my first real application. I need some advice in building page with three dependent model multiple choices fields or another similar interface. The goal is to build a page where user can choice target folders from three-level folders structure and save user selection in DB for further use. One of requirements is to choose L1,L2 and L3 dirs separately. I want to crate three list contains L1, L2 and L3 dirs names each, where user selects any available dirs. If user changes selection on L1 dirs list it need to automatically update L2 dirs list, the same rule in L2 dirs list for L3 dirs list. Due to security restrictions I can't access filesystem with dirs from Django server. But it can be accessed from user PC. I decided to manually load dirs structure, because it is rarely changes.
The question is how to embed those lists at page and make them dependent. I guess it must be three model multiple choices fields with some JavaScript event to update them. But I need example how to build it. May be there is another better way than use ModelMultipleChoiceField...
Example of dirs - tree structure:
|-L1_Dir#1 -|-L2_Dir#A-|-L3_Dir#P
| | |-L3_Dir#Q
| |
| |-L2_Dir#B-|-L3_Dir#R
| |-L3_Dir#S
|
|-L1_Dir#2 -|-L2_Dir#C-|-L3_Dir#T
|-L3_Dir#U
I created a models.py structure:
from django.db import models
# Create your models here.
class L1_dir(models.Model):
name = models.CharField(max_length = 48)
def __str__(self):
return(self.name)
class L2_dir(models.Model):
name = models.CharField(max_length = 48)
l1_dir = models.ForeignKey(L1_dir, on_delete = models.CASCADE)
def __str__(self):
return(self.name)
class L3_dir(models.Model):
path = models.CharField(max_length = 256) #store full path here
name = models.CharField(max_length = 48)
l2_dir = models.ForeignKey(L2_dir, on_delete = models.CASCADE)
def __str__(self):
return(self.dir_name)
Upvotes: 2
Views: 86
Reputation: 145
You can use one model to handle this situation.
class LocationDir(models.Model):
path = models.CharField(max_length = 256)
name = models.CharField(max_length = 48)
directory = models.ForeignKey('self', on_delete = models.CASCADE, null=True, blank=True)
def __str__(self):
return self.name
Upvotes: 1