Django:从表中删除项目,但保留已删除项目的记录

时间:2018-07-20 17:28:59

标签: django django-models django-views

我正在为车间中的工具制作库存应用程序。我创建了一个视图,用于管理工具箱中的工具以及将工具箱分配给谁的视图,我可以使用 CreateView 类在工具箱中创建工具,但是我不知道如何删除框中的工具。

(让我们说其中一个工具坏了,我们必须替换它,我想保留该交易的记录,它在哪个购物车中以及丢弃该物品的原因。我还想检查一下它是否是一次性物品,或者是消耗性物品(如果该物品由于使用不当而损坏,或者该物品磨损到无法使用的状态)。

我该如何解决这个问题?

用于创建工具车(便携式)的模型

=========================================================================== #
#   MODELO PARA CREAR CARRITOS
# =========================================================================== #


class Carritos(models.Model):
    no_carrito = models.CharField(max_length=3, unique=True)
    empleado = models.OneToOneField(Empleados, on_delete=models.CASCADE)
    # empleado = models.ManyToManyField(Empleados,  through='Transaccion')
    items = models.ManyToManyField(Item, through='Transaccion', related_name='carritos')
    f_creacion = models.DateTimeField(auto_now_add=True)
    f_actualizacion = models.DateTimeField(auto_now=True)
    activo = models.BooleanField(default=True)

    def get_absolute_url(self):
        return reverse('inventario:carrito')#, kwargs={'pk': self.pk})


    class Meta:
        verbose_name_plural = "Carritos"

    def __str__(self):
        return self.no_carrito



class Transaccion(models.Model):

    carrito = models.ForeignKey(Carritos, on_delete=models.CASCADE, related_name='items_carrito')
    Herramienta = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='items_carrito')
    cantidad = models.PositiveSmallIntegerField()


    def get_absolute_url(self):
        return reverse('inventario:carrito')#, kwargs={'pk': self.pk})

views.py

# =========================================================================== #
#   LOGICA PARA CREAR CARRITOS
# =========================================================================== #
# ===================> Logica relacinado con Cortadores <=====================#

def create_carrito(request):
    template_name = 'inventario/carrito/createcarrito.html'
    model  = Carritos

    carritos = Carritos.objects.all()
    if carritos:
        return render(request, template_name, {'carritos':carritos})

    else:
        return render(request,template_name)


class CarritoCreate(CreateView):
    model = Carritos
    fields = [
        'no_carrito',
        'empleado',
        'activo',
    ]

class ItemCreate(CreateView):
    model = Transaccion
    fields = [
        'carrito',
        'Herramienta',
        'cantidad',
    ]


def detalle_carrito(request, pk):
    model = Carritos, Transaccion
    template_name = 'inventario/carrito/detalles_carrito.html'

    carritos = Carritos.objects.all().filter(pk=pk)
    trans =Transaccion.objects.filter(carrito=pk)

    return render(request,template_name, {'carrito':carritos, 'trans':trans} )


class CarritoUpdate(UpdateView):
    model = Carritos

    fields = [
        'no_carrito',
        'empleado',
        'activo',
    ]
    template_name_suffix = '_update_form' 

1 个答案:

答案 0 :(得分:0)

在Carritos中添加一个这样的文件(与Transaccion具有父关系)

Carritos(models.Model)类:

router.post('/saveNewsPost', function(req, res){
    console.log(req.body);

    var newPost = new NewsArticle({
        htmlCode: req.body
    });

    newPost.save(function(err, newsArticle){
        if (err) {
            return res.status(500).json({
                title: "An error occurred",
                error: err
            })
        }
        res.status(200).json({
            message: "Succesfully saved the newsArticle",
            obj: newsArticle
        })
    })
});