last id from upload photo

Hi, my problem populate "photo_order = " in last id :

class CaricaFotoNelAlbum(TemplateView):
    model = Photo
    context_object_name = 'photo'
    def get(self, request, album_slug):
        orgs = Album.objects.get(slug=album_slug)
        if not orgs:
            return render(request, 'photo_404.html')
        else:
            photo = Photo.objects.filter(album__slug=album_slug)
            album = Album.objects.filter(slug__isnull = False, slug=album_slug).order_by('created')
        return render(request, 'photo_upload.html', {'photo': photo, 'album': album})

    def post(self, request, album_slug):
        if request.method == 'POST':
            album = Album.objects.get(slug=album_slug)
            my_file=request.FILES.get('file')
            p = Photo.objects.create(
            author=request.user,
            content='',
            album=album,
            photo=my_file,
            photo_order = '',  # Insert last ID photo ID.
            status=0)
            return p, HttpResponse('')
        return JsonResponse({'photo':'false'})

How to solved problem ?
thanks

This is actually one of those situations where you’re creating more work for yourself than necessary.

Based only on the information you’ve posted here so far, I wouldn’t bother tracking a “photo_order” sequence number.

I would suggest creating that field as a DateTimeField with auto_now_add=True. That lets you establish the order in which the photos were added.

Hi, thanks my models.py:

from django.db import models
from stdimage import StdImageField
from django.utils.text import slugify
from django.urls import reverse
from django.utils.translation import gettext as _
import os
from . utils import path_and_rename, resize_and_autorotate
from django.conf import settings
from datetime import *


class Album(models.Model):
    id = models.AutoField(primary_key=True)
    album_order = models.PositiveBigIntegerField(null=True, blank=True, verbose_name=('Album order'))
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name=_('Author') )
    title = models.CharField(max_length=255, verbose_name=_('Title'))
    content = models.TextField(verbose_name=_('Content'))
    slug = models.SlugField(null=False, unique=True, max_length=255, verbose_name=_('Slug'))
    status = models.BooleanField(default=0, verbose_name=_('Status'))
    created = models.DateTimeField(auto_now_add=True, verbose_name=_('Created'))
    updated = models.DateTimeField(auto_now=True, verbose_name=_('Updated'))


    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('album', kwargs={'slug': self.slug})



    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super().save(*args, **kwargs)

    class Meta:
        ordering = ['id']


class Photo(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name=_('Author'))
    content = models.CharField(max_length=255, verbose_name=_('Content'))
    album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='ALBUM')
    photo = StdImageField(upload_to=path_and_rename, blank=True, render_variations= resize_and_autorotate, variations={
        'large': (640, 480),
        'thumbnail': (320, 220, True),
    }, delete_orphans=True, verbose_name=_('Photo'))
    photo_order = models.PositiveBigIntegerField(null=True, blank=True,verbose_name=('Photo order'))
    status = models.BooleanField(default=0, verbose_name=_('Status'))
    created = models.DateTimeField(auto_now_add=True, verbose_name=_('Created'))
    updated = models.DateTimeField(auto_now=True, verbose_name=_('Updated'))

    def delete(self, *args, **kwargs):
        os.remove(os.path.join(settings.MEDIA_ROOT, self.photo.name))
        super().delete(*args, **kwargs)

    def save(self, *args, **kwargs):
        return super(Photo, self).save(*args, **kwargs)
    
    def __str__(self):
        return f'{self.id}'
    class Meta:
        ordering = ['id']

solution models Photo and Album ?,

my album_order and photo_order

idea solution ?

thanks.
My elementary english from italian language old 40 year.

row “photo_order” my writing code javascript up/down photo

Like : Youtube in the playlist video like position up/down video…

My studing replace code in my gallery photo.

Ok, then get the current DateTime as an integer, and use it as the initial value.

When you reorder, you’re going to reassign indexes anyway, so it doesn’t matter what the original number was.

example code ??? thanks

See time — Time access and conversions — Python 3.10.5 documentation for some options and alternatives.

If I were making a choice here, I’d probably use time.time_ns().

problem code:

    def save(self, *args, **kwargs):
        self.photo_order = Photo.objects.filter(id=self.id).update(photo_order=F("photo_order") + 1)
        return super(Photo, self).save(*args, **kwargs)

not increment photo_order integer field . problem solution???

thanks.

Look at this expression:

You’re setting the variable named photo_order to what that expression returns.

What’s going to be the return value of that expression?

Hint: See update.