Skip to content

Instantly share code, notes, and snippets.

@richellyitalo
Last active April 17, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richellyitalo/0038d9bb77f4da1cd8421dfd022959ba to your computer and use it in GitHub Desktop.
Save richellyitalo/0038d9bb77f4da1cd8421dfd022959ba to your computer and use it in GitHub Desktop.

Pegar último registro

# Maneira 1
last_banner = Banner.objects.order_by('-pk')[:1]
if last_banner.exists():
    instance['sort_order'] = last_banner[0].sort_order + 1
    
# Maneira 2
# Pode gerar erro caso não exista registro
last_banner = Banner.objects.latest('id')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment