How can I get access to deleted objects in Django?

Asked

Viewed 79 times

0

I’m developing a parking management system in Django that registers the 'Vehicle' object according to its time of entry, and calculates the value as it goes, but I need to develop a reporting system that returns how many cars came in and the total value of the day.

How can I get access to the vehicles that left the parking, because after the exit record, the object is deleted in the database.

# models.py
class Veiculo(models.Model):
    marca = models.CharField(max_length=100, null=False)
    modelo = models.CharField(max_length=100, null=False)
    cor = models.CharField(max_length=50, null=False)
    placa = models.CharField(max_length=8, null=False)
    horario = models.DateTimeField(auto_now_add=True, null=False)

    def __str__(self):
        return self.placa


class ValorEstacionamento(models.Model):
    periodoManha = MoneyField(max_digits=14, decimal_places=2,
                              default_currency='BRL', default='2')
    periodoTarde = MoneyField(max_digits=14, decimal_places=2,
                              default_currency='BRL', default='3')
    periodoFDS = MoneyField(max_digits=14, decimal_places=2,
                            default_currency='BRL', default='2.5')

    def __str__(self):
        return "Tabela de Valores"
# views.py
class EntradaCreateView(CreateView):
    model = Veiculo
    form_class = InsereVeiculoForm
    template_name = 'entrada.html'
    success_url = reverse_lazy('sistema:veiculos')


class VeiculosListView(ListView):
    template_name = 'veiculos.html'
    model = Veiculo
    context_object_name = 'veiculos'


def saida(request, placa):
    veiculo = get_object_or_404(Veiculo, placa=placa)
    veiculo.delete()
    return render(request, 'index.html')
# forms.py
class InsereVeiculoForm(forms.ModelForm):

    class Meta:
        model = Veiculo
        fields = [
            'marca',
            'modelo',
            'cor',
            'placa',
        ]
  • Missing data, where vehicle entry class and vehicle exit class

  • I performed the entry and exit of the vehicles in views.py

1 answer

0


To query this data, you will need to log it somewhere. I suggest that instead of deleting the vehicle after exit, only the "deactivate", you can add an "active" Boolean attribute to your model, this way you will deactivate the car by marking the attribute as True or False. If you want to do it a little more laborious, but more correct, you can create a new model where you will record the entries and exits of the vehicle, so the registration will always be in your vehicle table, and, in another table, you will record only the date of entry and date of exit of that linked vehicle the same in this record through a foreign key.

  • 1

    Let me get this straight... For the Date Log, I must create a class Data(models.Model): who receives the data_entrada = models.ForeignKey(Veiculo, on_delete=models.CASCADE) and data_saida = models.DateTimeField(auto_now_add=True). And for the Entry and Exit Registration of the vehicle, I didn’t quite understand how to structure the model.

  • You will create a Data class like you said, and create something like: data_input = models.Datetimefield('Input date', auto_now_add=True) data_output = models.Datetimefield('Output date', auto_now=True) vehicle = models.Foreignkey('nameApp.Vehicle', on_delete=models.PROTECT) .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.