from django.contrib.admin import SimpleListFilter
class ArhivFilter(SimpleListFilter):
title = 'Архив' # or use _('country') for translated title
parameter_name = 'in_arhiv'
def lookups(self, request, model_admin):
self.model = model_admin.model
return [
("in_arhive", "В архиве"),
("", "Не в архиве")
]
def queryset(self, request, queryset):
if self.value() == 'in_arhive':
return self.model.objects.show_arhiv()
else:
return queryset
@admin.register(TimeTask)
class TimeTask(admin.ModelAdmin):
list_display = ("time", "time_task_date", "task")
list_filter = (ArhivFilter,)
@admin.register(ProjectWorkPlan)
class ProjectWorkPlanAdmin(admin.ModelAdmin):
def view_time(self, obj):
count = obj.timetask_set.count()
url = (
reverse("admin:management_timetask_changelist")
+ "?"
+ urlencode({"in_arhive": f"{obj.id}"})
reverse("admin:management_timetask_changelist")
+ "?"
+ urlencode({"task": f"{obj.id}"} или + urlencode({"task__id": f"{obj.id}"}
class ColorChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return format_html("{} <b>{}</b>",
mark_safe("<img src=%s style='max-width: 100px;'/>" % obj.image.url),
obj.prop
)
# return mark_safe("<img src='%s' style='max-width: 100px;'/>" % (obj.image.url) )
class CurrentProductModelAdmin(admin.ModelAdmin):
list_display = '__str__', 'product', 'color', 'packing', 'price',
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'color':
return ColorChoiceField(queryset=Color.objects.all())
return super().formfield_for_foreignkey(db_field, request, **kwargs)
return format_html("{} <b>{}</b>",
mark_safe("<img src='%s' style='max-width: 100px;'/>" % obj.image.url),
obj.prop
)
<div class="form-row field-color">
<div>
<label class="required" for="id_color">Color:</label>
<div class="related-widget-wrapper" data-model-ref="Цвет">
<select name="color" required="" id="id_color">
<option value="">---------</option>
<option value="1"> Зелёный</option>
<option value="2"> Красный</option>
<option value="3" selected=""> Желтый</option>
</select>
<a class="related-widget-wrapper-link change-related" id="change_id_color" data-href-template="/admin/product/color/__fk__/change/?_to_field=id&_popup=1" data-popup="yes" title="Изменить выбранный объект типа " Цвет""="" href="/admin/product/color/3/change/?_to_field=id&_popup=1"><img src="/static/admin/img/icon-changelink.svg" alt="Изменить"></a><a class="related-widget-wrapper-link add-related" id="add_id_color" data-popup="yes" href="/admin/product/color/add/?_to_field=id&_popup=1" title="Добавить ещё один объект типа " Цвет""=""><img src="/static/admin/img/icon-addlink.svg" alt="Добавить"></a><a class="related-widget-wrapper-link delete-related" id="delete_id_color" data-href-template="/admin/product/color/__fk__/delete/?_to_field=id&_popup=1" data-popup="yes" title="Удалить выбранный объект типа " Цвет""="" href="/admin/product/color/3/delete/?_to_field=id&_popup=1"><img src="/static/admin/img/icon-deletelink.svg" alt="Удалить"></a><a class="related-widget-wrapper-link view-related" id="view_id_color" data-href-template="/admin/product/color/__fk__/change/?_to_field=id" title="View selected Цвет" href="/admin/product/color/3/change/?_to_field=id"><img src="/static/admin/img/icon-viewlink.svg" alt="Просмотреть"></a>
</div>
</div>
</div>
class Color(models.Model):
"""модель цвета для товара"""
autoregister_model=True
prop = models.CharField(max_length=500, verbose_name='Описание цвета')
image = ThumbnailerImageField( upload_to=upload_color_path_handler, verbose_name='Изображение',
resize_source=dict(size=(72, 72), sharpen=True))
class Meta:
verbose_name = "Цвет"
verbose_name_plural = "Цвета"
def __str__(self):
return f'{self.prop}'
class CurrentProduct(models.Model):
"""конкретный товар цвет-фасовка-цена"""
vendorСode = models.CharField(max_length=100, verbose_name='Артикул', primary_key=True)
price = models.PositiveIntegerField(verbose_name='Цена', default='0')
packing = models.ForeignKey(Packing, verbose_name='Фасовка',
related_name='current_products', on_delete=models.SET_NULL,
null=True, blank=True)
color = models.ForeignKey(Color, verbose_name='Цвет',
related_name='current_products', on_delete=models.SET_NULL,
null=True, blank=True)
product = models.ForeignKey(Product, related_name='current_products', verbose_name='Товар',
on_delete=models.SET_NULL, null=True, blank=True)
storages = models.ManyToManyField(Storage, verbose_name='Склады с наличием',blank=True)
class Meta:
verbose_name = "Конкретный товар"
verbose_name_plural = "Конкретные товары"
ordering = ['product']
def __str__(self):
return self.vendorСode
DEBUG_TOOLBAR_CONFIG = {
'SHOW_TOOLBAR_CALLBACK': lambda r: False, # disables it
# '...
}
Дает такую ошибку
Пробовал ТАК:
И опять 25, если OK() то КОД работает, НO ЕСЛИ Err(), то log("123"); даже не выводится