S
S
Sergey Maysk2018-02-19 00:28:54
Django
Sergey Maysk, 2018-02-19 00:28:54

How to filter values ​​of related fields in django admin?

models.py

class GoodOptions(models.Model):
    option = models.CharField(max_length=32, default=None, verbose_name=u'Характеристика', blank=False)

    class Meta:
        verbose_name = u'Характеристика'
        verbose_name_plural = u'Характеристики'

    def __str__(self):
        return self.option
    
class OptionValue(models.Model):
    value = models.CharField(max_length=255, blank=True, verbose_name=u'Значение опции', default=u'Значение')
    option = models.ForeignKey('GoodOptions', verbose_name=u'Характеристика', default=None, on_delete=models.CASCADE)

    class Meta:
        verbose_name = (u'Значение характеристики')
        verbose_name_plural = (u'Значения характеристик')

    def __str__(self):
        return self.value
    
class OptionsDef(models.Model):
    good = models.ForeignKey('Products', verbose_name=u'Товар', default=None, on_delete=models.CASCADE)
    option = models.ForeignKey('GoodOptions', verbose_name=u'Характеристика', default=None, on_delete=models.CASCADE)
    value = models.ForeignKey('OptionValue', verbose_name=u'Значение', default=None, on_delete=models.CASCADE)

    class Meta:
        verbose_name = (u'Значение характеристики товара')
        verbose_name_plural = (u'Значения характеристик товаров')

    def __str__(self):
        return self.option.option
    
forms.py

class MyOptionsDefForm(forms.ModelForm):
    class Meta:
        model = OptionsDef
        fields = '__all__'
    
  def clean(self):
        cleaned_data = super(MyOptionsDefForm, self).clean()
        sel_option = cleaned_data.get("option") # получить выбранное из выпадающего списка значение характеристики
    option_id = sel_option.id  # получить id характеристики 
    value = OptionValue.objects.filter(option__optionvalue__option=option_id) # получить отфильтрованные по характеристике значения
    self.value = value
    return self.cleaned_data
    
admin.py

from catalog.forms import MyOptionsDefForm
class OptionsDefAdmin(admin.ModelAdmin):
    list_display = ('option', 'good')
    form = MyOptionsDefForm
admin.site.register(OptionsDef, OptionsDefAdmin)

It is necessary to make it so that in the admin panel, after selecting from the drop-down list of Characteristics, in the drop-down list of Values, only those that belong to this characteristic are shown. And now I drop out all the Values, regardless of the selected Characteristic. I suppose to solve the problem by redefining the form in the admin panel and the def clean function - but with the current code, the admin panel does not react to it at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-02-19
@maysk

The path of a web developer must begin with the understanding that a website is not really one application, but two, written in different languages, running on different computers, in different environments and at different times.
In the case of Django, the operation of the site can be broken down into the following steps:
All further user actions will be processed only in the browser, by the browser itself and/or javascript. Both javascript and the browser will work with the data received from the server. If you need other data, you will have to repeat the operations from the list above, either by submitting a form or using an ajax request.
Hence the answer to your question, if you want to change the data in the drop-down lists of the form, you will have to write a javascript form field change handler that makes ajax requests to the server and changes the values ​​\u200b\u200bof the form fields in accordance with the data received from the server, as well as additional views on server to process these ajax requests. For these purposes, you can try using a ready-made solution - django-select2 .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question