From c8f4ab1a80c24f2edce3e110b62847e93f41efa1 Mon Sep 17 00:00:00 2001 From: Qwann Date: Wed, 8 Aug 2018 16:02:23 +0200 Subject: [PATCH] categories as taxinomy --- equipment/admin.py | 17 +++++------ .../migrations/0005_auto_20180808_1013.py | 28 +++++++++++++++++++ .../migrations/0006_auto_20180808_1354.py | 21 ++++++++++++++ equipment/models.py | 24 +++++----------- 4 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 equipment/migrations/0005_auto_20180808_1013.py create mode 100644 equipment/migrations/0006_auto_20180808_1354.py diff --git a/equipment/admin.py b/equipment/admin.py index bcadaa1..1442f72 100644 --- a/equipment/admin.py +++ b/equipment/admin.py @@ -1,11 +1,12 @@ from django.contrib import admin 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 shared.admin import admin_site + class IdForm(forms.ModelForm): def __init__(self, *args, **kwargs): if 'min_value' in kwargs: @@ -23,6 +24,7 @@ class IdForm(forms.ModelForm): self.fields[field.name].choices = choices self.fields[field.name].widget = IdWidget(choices=self.fields[field.name].choices) + class IdFormset(forms.models.BaseInlineFormSet): def get_form_kwargs(self, index): kwargs = super().get_form_kwargs(index) @@ -55,20 +57,19 @@ class EquipmentLostExtraInline(admin.TabularInline): classes = ['collapse'] -class CharFieldModelAdmin(admin.ModelAdmin): - list_display = ['name'] - ordering = ['name'] +class CategoryAdmin(admin.ModelAdmin): + list_display = ['name', 'parent'] + ordering = ['name', 'parent'] class EquipmentAdmin(admin.ModelAdmin): - list_display = ['name', 'stock', 'owner', 'pole', 'category'] - ordering = ['name', 'owner', 'pole', 'category'] + list_display = ['name', 'stock', 'owner', 'category'] + ordering = ['name', 'owner', 'category'] inlines = [EquipmentDefaultExtraInline, EquipmentLostExtraInline, EquipmentRevisionExtraInline] admin_site.register(Equipment, EquipmentAdmin) -admin_site.register(EquipmentPole, CharFieldModelAdmin) -admin_site.register(EquipmentCategory, CharFieldModelAdmin) +admin_site.register(EquipmentCategory, CategoryAdmin) admin.site.register(Equipment, EquipmentAdmin) diff --git a/equipment/migrations/0005_auto_20180808_1013.py b/equipment/migrations/0005_auto_20180808_1013.py new file mode 100644 index 0000000..2a3491c --- /dev/null +++ b/equipment/migrations/0005_auto_20180808_1013.py @@ -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', + ), + ] diff --git a/equipment/migrations/0006_auto_20180808_1354.py b/equipment/migrations/0006_auto_20180808_1354.py new file mode 100644 index 0000000..e5625c0 --- /dev/null +++ b/equipment/migrations/0006_auto_20180808_1354.py @@ -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'), + ), + ] diff --git a/equipment/models.py b/equipment/models.py index 9b47dff..aafc5e7 100644 --- a/equipment/models.py +++ b/equipment/models.py @@ -10,25 +10,18 @@ from taggit.managers import TaggableManager 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): name = models.CharField( _("nom"), 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: verbose_name = _("catégories") @@ -55,9 +48,6 @@ class Equipment(EventSpecificMixin, models.Model): blank=True, null=True, ) - pole = models.ForeignKey( - EquipmentPole, - ) category = models.ForeignKey( EquipmentCategory, )