mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-05 05:05:17 +02:00
created_at and updated_at fields
This commit is contained in:
parent
4abaaa5fb3
commit
c6633a17cb
11 changed files with 81 additions and 39 deletions
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 5.0.6 on 2024-07-19 12:55
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('adventures', '0010_adventure_created_at_collection_created_at'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='adventure',
|
||||
name='updated_at',
|
||||
field=models.DateTimeField(auto_now=True),
|
||||
),
|
||||
]
|
|
@ -36,6 +36,7 @@ class Adventure(models.Model):
|
|||
latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
|
||||
collection = models.ForeignKey('Collection', on_delete=models.CASCADE, blank=True, null=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def clean(self):
|
||||
if self.collection:
|
||||
|
|
|
@ -31,11 +31,11 @@ class AdventureViewSet(viewsets.ModelViewSet):
|
|||
pagination_class = StandardResultsSetPagination
|
||||
|
||||
def apply_sorting(self, queryset):
|
||||
order_by = self.request.query_params.get('order_by', 'created_at')
|
||||
order_by = self.request.query_params.get('order_by', 'updated_at')
|
||||
order_direction = self.request.query_params.get('order_direction', 'asc')
|
||||
include_collections = self.request.query_params.get('include_collections', 'true')
|
||||
|
||||
valid_order_by = ['name', 'type', 'date', 'rating', 'created_at']
|
||||
valid_order_by = ['name', 'type', 'date', 'rating', 'updated_at']
|
||||
if order_by not in valid_order_by:
|
||||
order_by = 'name'
|
||||
|
||||
|
@ -52,12 +52,12 @@ class AdventureViewSet(viewsets.ModelViewSet):
|
|||
if order_direction == 'desc':
|
||||
ordering = f'-{ordering}'
|
||||
|
||||
# reverse ordering for created_at field
|
||||
if order_by == 'created_at':
|
||||
# reverse ordering for updated_at field
|
||||
if order_by == 'updated_at':
|
||||
if order_direction == 'asc':
|
||||
ordering = '-created_at'
|
||||
ordering = '-updated_at'
|
||||
else:
|
||||
ordering = 'created_at'
|
||||
ordering = 'updated_at'
|
||||
|
||||
print(f"Ordering by: {ordering}") # For debugging
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue