Я создал шаблон, представление, форму и модель, в которых все модели синхронизируются по одному с помощью внешних ключей, и теперь мне нужно выбрать 3 раскрывающихся списка в зависимости от вариантов, которые я определил в предыдущих раскрывающихся списках, но после этого раскрывающиеся списки пусты без данных, но есть наборы запросов, а также form.media. js и form.media. css. Ниже представлены формы и модели.
class GoodDistributorLocationCreateForm(ModelForm):
good = ModelChoiceField(
queryset=Good.objects.all(),
label=u"Good",
widget=ModelSelect2Widget(model=Good, search_fields=['name__icontains'],),
)
distributor = ModelChoiceField(
queryset=Distributor.objects.all(),
label=u"Distributor",
widget=ModelSelect2Widget(
model=Distributor, search_fields=['name__icontains'], dependent_fields={'good': 'good'},
),
)
location = ModelChoiceField(
queryset=Location.objects.all(),
label=u"Location",
widget=ModelSelect2Widget(
model=Location,
search_fields=['address__icontains'],
dependent_fields={'distributor': 'distributor'},
max_results=500,
),
)
class Meta:
"""Add good distributor location form."""
model = GoodDistributorLocation
fields = ['good', 'distributor', 'location', 'price']
def __init__(self, *args, **kwargs):
"""Initialize form for setting good distributor location request fields."""
super(GoodDistributorLocationCreateForm, self).__init__(*args, **kwargs)
self.fields['good'].required = True
self.fields['distributor'].required = True
self.fields['location'].required = True
self.fields['price'].required = True
Модели:
class Good(models.Model):
TYPE_TUKTUK = 0
TYPE_CHOICES = ((TYPE_TUKTUK, 'TukTuk'),)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=50)
good_type = models.IntegerField(choices=TYPE_CHOICES, default=TYPE_TUKTUK)
brand = models.CharField(max_length=255, blank=True)
def __str__(self):
"""
Unicode representation for a Good model.
:return: string
"""
return '{} - {}'.format(self.name, self.get_good_type_display())
@property
def shortened_id(self):
"""Get shortened version of id."""
return str(self.id)[-8:]
class Distributor(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=50)
description = models.TextField(blank=True)
def __str__(self):
"""
Unicode representation for a Distributor model.
:return: string
"""
return '{}'.format(self.name)
@property
def shortened_id(self):
"""Get shortened version of id."""
return str(self.id)[-8:]
class Location(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=50)
address = models.CharField(max_length=255, blank=True)
def __str__(self):
"""
Unicode representation for a Location model.
:return: string
"""
return '{} - {}'.format(self.name, self.address)
@property
def shortened_id(self):
"""Get shortened version of id."""
return str(self.id)[-8:]
class GoodDistributorLocation(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
good = models.ForeignKey('products.Good', on_delete=models.CASCADE, related_name='TukTuk')
distributor = models.ForeignKey('products.Distributor', on_delete=models.CASCADE)
location = models.ForeignKey('products.Location', on_delete=models.CASCADE)
price = models.DecimalField(max_digits=16, decimal_places=2)
@property
def shortened_id(self):
"""Get shortened version of id."""
return str(self.id)[-8:]