Guest User

Untitled

a guest
Jul 13th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. class SelectWOA(Select):
  2.     def create_option(self, name, value, label, selected, index,
  3.                       subindex=None, attrs=None):
  4.         option_dict = super(SelectWOA, self).create_option(name, value,
  5.             label, selected, index, subindex=subindex, attrs=attrs)
  6.             #Category.objects.
  7.         try:
  8.             option_dict['attrs']['style'] =  'color: ' +  Category.objects.get(name=label).color + ';'
  9.         except:
  10.             pass
  11.            
  12.         return option_dict
  13.  
  14. from django.forms import ChoiceField, ModelChoiceField
  15. from category.models import Category
  16. class ProductAdminForm(ModelForm):
  17.     test_field = ModelChoiceField(queryset=Category.objects.all(), widget=SelectWOA())
  18.     class Meta:
  19.         model = Product
  20.         exclude = ['category']
  21.         fields = ('name', 'image', 'image2', 'category', 'test_field',)
  22.  
  23.  
  24.  
  25.  
  26.  
  27. from django.db import models
  28. class ProductCastumAdmin(admin.ModelAdmin):
  29.     #fields = ('name',)
  30.     form = ProductAdminForm
Add Comment
Please, Sign In to add comment