在管理中显示选择字段作为输入字段
Display choicefield in admin as input field
我有一个大约包含 100 个用户的数据库,每个用户都有 5-10k 个产品链接到他们。由于产品太多,在管理面板中加载该页面真的很慢。所以我想做的是用正则表达式或至少一个不预加载所有产品的数字输入字段替换它:
型号:
class Store(models.Model):
name = models.CharField("name", max_length = 128)
user = models.OneToOneField(User, on_delete = models.CASCADE, )
testproduct = models.Foreignkey(Product, on_delete = models.CASCADE, null = True, blank = True)
class Product(models.Model):
data = models.JSONField()
number = models.PositiveIntegerField()
store = models.ForeignKey(Store, on_delete = models.CASCADE)
管理员:
class StoreAdmin(admin.ModelAdmin):
list_display = ["name", ...]
raw_id_fields = ["testproduct", ...]
通过这种方式,我在管理页面上获得了一个 ID 输入字段:
有什么方法可以使它成为正则表达式字段,以便我可以搜索我产品的 data
属性以及 number
属性?
我认为,实现您的目标的最佳方法是在您的模型管理表单中添加额外的字段并覆盖它的保存方法,试试这个:
#admin.py
from django import forms
class StoreAdminModelForm(forms.ModelForm):
regex_field = forms.CharField()
def save(self, commit=True):
product_regex = self.cleaned_data.get('regex_field', None)
instance = super(StoreAdminModelForm, self).save(commit=commit)
# put some more regex validations here
if len(product_regex) > 0:
product = Product.objects.filter(data__regex=product_regex).first()
if product:
instance.testproduct = product
else:
raise forms.ValidationError('Product with data satisfying regex was not found!')
if commit:
instance.save()
return instance
class Meta:
model = Store
fields = '__all__'
class StoreAdmin(admin.ModelAdmin):
form = StoreAdminModelForm
raw_id_fields = ["testproduct", ]
fields = ('name ', 'user', 'testproduct', 'regex_field')
readonly_fields = ('user', )
因此,结果 - 您将拥有正则表达式的特殊字段,您将尝试根据该字段获取测试产品(不触及 raw_id_field)
我有一个大约包含 100 个用户的数据库,每个用户都有 5-10k 个产品链接到他们。由于产品太多,在管理面板中加载该页面真的很慢。所以我想做的是用正则表达式或至少一个不预加载所有产品的数字输入字段替换它:
型号:
class Store(models.Model):
name = models.CharField("name", max_length = 128)
user = models.OneToOneField(User, on_delete = models.CASCADE, )
testproduct = models.Foreignkey(Product, on_delete = models.CASCADE, null = True, blank = True)
class Product(models.Model):
data = models.JSONField()
number = models.PositiveIntegerField()
store = models.ForeignKey(Store, on_delete = models.CASCADE)
管理员:
class StoreAdmin(admin.ModelAdmin):
list_display = ["name", ...]
raw_id_fields = ["testproduct", ...]
通过这种方式,我在管理页面上获得了一个 ID 输入字段:
有什么方法可以使它成为正则表达式字段,以便我可以搜索我产品的 data
属性以及 number
属性?
我认为,实现您的目标的最佳方法是在您的模型管理表单中添加额外的字段并覆盖它的保存方法,试试这个:
#admin.py
from django import forms
class StoreAdminModelForm(forms.ModelForm):
regex_field = forms.CharField()
def save(self, commit=True):
product_regex = self.cleaned_data.get('regex_field', None)
instance = super(StoreAdminModelForm, self).save(commit=commit)
# put some more regex validations here
if len(product_regex) > 0:
product = Product.objects.filter(data__regex=product_regex).first()
if product:
instance.testproduct = product
else:
raise forms.ValidationError('Product with data satisfying regex was not found!')
if commit:
instance.save()
return instance
class Meta:
model = Store
fields = '__all__'
class StoreAdmin(admin.ModelAdmin):
form = StoreAdminModelForm
raw_id_fields = ["testproduct", ]
fields = ('name ', 'user', 'testproduct', 'regex_field')
readonly_fields = ('user', )
因此,结果 - 您将拥有正则表达式的特殊字段,您将尝试根据该字段获取测试产品(不触及 raw_id_field)