Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
Executing Django's sqlsequencereset code from within python

Django's sqlsequencereset command returns the SQL commands needed to reset sequences in the database.

Other similar management commands automatically execute the SQL by default (and only print out the output if you specify the --dry-run option).

From the command line you can execute the returned commands with:

python manage.py sqlsequencereset app1_name app2_name | python manage.py dbshell

But... Is there a way to directly execute the returned code from sqlsequencereset from inside python?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can execute the SQL query generated by sqlsequencereset from within python in this way (using the default database):

from django.core.management.color import no_style
from django.db import connection

from myapps.models import MyModel1, MyModel2


sequence_sql = connection.ops.sequence_reset_sql(no_style(), [MyModel1, MyModel2])
with connection.cursor() as cursor:
    for sql in sequence_sql:
        cursor.execute(sql)

I tested this code with Python3.6, Django 2.0 and PostgreSQL 10.

about 4 years ago · Santiago Trujillo Relatório

0

I'm currently managing this by:

  1. Using call_command to run sqlsequencereset
  2. Grabbing the output into an in-memory StringIO object (instead of writing to a file on disc)
  3. Executing the SQL directly against Django's default database

Code:

import io

from django.core.management import call_command
from django.db import connection


app_name = 'my_app'

# Get SQL commands from sqlsequencereset
output = io.StringIO()
call_command('sqlsequencereset', app_name, stdout=output, no_color=True)
sql = output.getvalue()
    
with connection.cursor() as cursor:
    cursor.execute(sql)

output.close()
about 4 years ago · Santiago Trujillo Relatório

0

also see [this gist][1], where it can be done e.g. from console for all apps.

import os

os.environ['DJANGO_COLORS'] = 'nocolor'

from django.core.management import call_command
from django.db import connection
from django.apps import apps
from io import StringIO

commands = StringIO()
cursor = connection.cursor()

for app in apps.get_app_configs():
    call_command('sqlsequencereset', app.label, stdout=commands)

cursor.execute(commands.getvalue())
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda