categories as taxinomy

This commit is contained in:
Qwann 2018-08-08 16:02:23 +02:00
parent 4c1a1da64f
commit c8f4ab1a80
4 changed files with 65 additions and 25 deletions

View file

@ -1,11 +1,12 @@
from django.contrib import admin from django.contrib import admin
from django import forms from django import forms
from .models import Equipment, EquipmentDefault, EquipmentRevision, EquipmentPole, EquipmentCategory, EquipmentLost from .models import Equipment, EquipmentDefault, EquipmentRevision, EquipmentCategory, EquipmentLost
from .fields import IdField, IdWidget from .fields import IdField, IdWidget
from shared.admin import admin_site from shared.admin import admin_site
class IdForm(forms.ModelForm): class IdForm(forms.ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if 'min_value' in kwargs: if 'min_value' in kwargs:
@ -23,6 +24,7 @@ class IdForm(forms.ModelForm):
self.fields[field.name].choices = choices self.fields[field.name].choices = choices
self.fields[field.name].widget = IdWidget(choices=self.fields[field.name].choices) self.fields[field.name].widget = IdWidget(choices=self.fields[field.name].choices)
class IdFormset(forms.models.BaseInlineFormSet): class IdFormset(forms.models.BaseInlineFormSet):
def get_form_kwargs(self, index): def get_form_kwargs(self, index):
kwargs = super().get_form_kwargs(index) kwargs = super().get_form_kwargs(index)
@ -55,20 +57,19 @@ class EquipmentLostExtraInline(admin.TabularInline):
classes = ['collapse'] classes = ['collapse']
class CharFieldModelAdmin(admin.ModelAdmin): class CategoryAdmin(admin.ModelAdmin):
list_display = ['name'] list_display = ['name', 'parent']
ordering = ['name'] ordering = ['name', 'parent']
class EquipmentAdmin(admin.ModelAdmin): class EquipmentAdmin(admin.ModelAdmin):
list_display = ['name', 'stock', 'owner', 'pole', 'category'] list_display = ['name', 'stock', 'owner', 'category']
ordering = ['name', 'owner', 'pole', 'category'] ordering = ['name', 'owner', 'category']
inlines = [EquipmentDefaultExtraInline, inlines = [EquipmentDefaultExtraInline,
EquipmentLostExtraInline, EquipmentLostExtraInline,
EquipmentRevisionExtraInline] EquipmentRevisionExtraInline]
admin_site.register(Equipment, EquipmentAdmin) admin_site.register(Equipment, EquipmentAdmin)
admin_site.register(EquipmentPole, CharFieldModelAdmin) admin_site.register(EquipmentCategory, CategoryAdmin)
admin_site.register(EquipmentCategory, CharFieldModelAdmin)
admin.site.register(Equipment, EquipmentAdmin) admin.site.register(Equipment, EquipmentAdmin)

View file

@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.11 on 2018-08-08 10:13
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('equipment', '0004_equipment_owner'),
]
operations = [
migrations.RemoveField(
model_name='equipment',
name='pole',
),
migrations.AddField(
model_name='equipmentcategory',
name='parent',
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='equipment.EquipmentCategory'),
),
migrations.DeleteModel(
name='EquipmentPole',
),
]

View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.11 on 2018-08-08 13:54
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('equipment', '0005_auto_20180808_1013'),
]
operations = [
migrations.AlterField(
model_name='equipmentcategory',
name='parent',
field=models.ForeignKey(blank=True, default=None, help_text='merci de ne pas faire de référence cyclique', null=True, on_delete=django.db.models.deletion.CASCADE, to='equipment.EquipmentCategory'),
),
]

View file

@ -10,25 +10,18 @@ from taggit.managers import TaggableManager
from datetime import date from datetime import date
class EquipmentPole(models.Model):
name = models.CharField(
_("nom"),
max_length=200,
)
class Meta:
verbose_name = _("pôle")
verbose_name_plural = _("pôle")
def __str__(self):
return self.name
class EquipmentCategory(models.Model): class EquipmentCategory(models.Model):
name = models.CharField( name = models.CharField(
_("nom"), _("nom"),
max_length=200, max_length=200,
) )
parent = models.ForeignKey(
'self',
blank=True,
null=True,
default=None,
help_text=_("merci de ne pas faire de référence cyclique"),
)
class Meta: class Meta:
verbose_name = _("catégories") verbose_name = _("catégories")
@ -55,9 +48,6 @@ class Equipment(EventSpecificMixin, models.Model):
blank=True, blank=True,
null=True, null=True,
) )
pole = models.ForeignKey(
EquipmentPole,
)
category = models.ForeignKey( category = models.ForeignKey(
EquipmentCategory, EquipmentCategory,
) )