From 71583894bb6a789494f2abc33e7d3749a4f4f818 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Tue, 7 Mar 2023 22:20:34 +0800 Subject: [PATCH] Include username in logs endpoint --- ...alproduct_changed_by_product_changed_by.py | 26 +++++++++++++++++++ ivy/products/models.py | 2 ++ ivy/products/serializers.py | 5 ++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 ivy/products/migrations/0003_historicalproduct_changed_by_product_changed_by.py diff --git a/ivy/products/migrations/0003_historicalproduct_changed_by_product_changed_by.py b/ivy/products/migrations/0003_historicalproduct_changed_by_product_changed_by.py new file mode 100644 index 0000000..5586ff7 --- /dev/null +++ b/ivy/products/migrations/0003_historicalproduct_changed_by_product_changed_by.py @@ -0,0 +1,26 @@ +# Generated by Django 4.1.7 on 2023-03-07 14:13 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('products', '0002_historicalproduct'), + ] + + operations = [ + migrations.AddField( + model_name='historicalproduct', + name='changed_by', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to=settings.AUTH_USER_MODEL), + ), + migrations.AddField( + model_name='product', + name='changed_by', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/ivy/products/models.py b/ivy/products/models.py index aec772c..a372531 100644 --- a/ivy/products/models.py +++ b/ivy/products/models.py @@ -8,6 +8,8 @@ class Product(models.Model): name = models.CharField(max_length=20) quantity = models.IntegerField(default=0) date_added = models.DateTimeField(default=now, editable=False) + changed_by = models.ForeignKey( + 'auth.user', null=True, on_delete=models.SET_NULL) history = HistoricalRecords() def __str__(self): diff --git a/ivy/products/serializers.py b/ivy/products/serializers.py index 453b198..b50092e 100644 --- a/ivy/products/serializers.py +++ b/ivy/products/serializers.py @@ -26,13 +26,14 @@ class ProductSerializer(serializers.HyperlinkedModelSerializer): class LogSerializer(serializers.HyperlinkedModelSerializer): history_date = serializers.DateTimeField( format="%d-%m-%Y %I:%M%p", read_only=True) + history_user = serializers.ReadOnlyField(source='history_user.username') class Meta: model = Product.history.model fields = ('history_id', 'id', 'name', 'quantity', - 'history_date', 'history_user_id') + 'history_date', 'history_user') read_only_fields = ('history_id', 'id', 'name', 'quantity', - 'history_date', 'history_user_id') + 'history_date', 'history_user') class UserSerializer(serializers.ModelSerializer):