Include username in logs endpoint

This commit is contained in:
keannu125 2023-03-07 22:20:34 +08:00
parent dd115197f4
commit 71583894bb
3 changed files with 31 additions and 2 deletions

View file

@ -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),
),
]

View file

@ -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):

View file

@ -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):