自定义DetailView之类的函数返回相同的数据集-Django

时间:2018-07-26 17:13:40

标签: django django-models django-views

我有一个包含项目列表的视图,并且想要创建这些项目的详细视图,以便我可以更新,启用或禁用项目,但我遇到的问题是无论我使用哪个项目,我总是会获得相同的数据单击以获取详细信息。

我已经在这里多次张贴了此内容,但没有答案,也许我无法解释我的问题。

用于处理详细信息的视图

def detalle_Items(request, pk):
    model = Transaccion
    template_name = 'inventario/carrito/test-template.html'
    try:

        items_update = Transaccion.objects.filter(activo=True, carrito_id=pk, herramienta_id=pk)
    except Transaccion.DoesNotExist:
        raise Http404()

    return render(request, template_name, {'items_update':items_update})

这是绿色“ actualizar” 按钮

触发函数的地方

enter image description here

无论我按什么按钮,这都是我得到的:

enter image description here

这是我的用于carrito的 views.py 一组功能:

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

def home_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',
    ]


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

    carritos = Carritos.objects.filter(pk=pk)
    # GEST ALL TOOLS ASSIGN TO CARRITO'S PK THAT ARE ACTIVE 
    # TRY TO GET ALL ACTIVE ITEMS THAT BELONG TO CARRITO = PK AND AGREGATE TOTAL ITEMS PER TYPE
    cantidades = Transaccion.objects.values('herramienta__description').annotate(Sum('cantidad')).filter(activo=True, carrito_id=pk)

    # GEST ALL TOOLS ASSIGN TO CARRITO'S PK THAT ARE NOT ACTIVE 
    eliminados = Transaccion.objects.filter(activo=False,carrito_id=pk)

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



class CarritoUpdate(UpdateView):
    model = Carritos
    fields = [
        'no_carrito',
        'empleado',
        'activo',
    ]
    template_name_suffix = '_update_form' 




def detalle_Items(request, pk):
    model = Transaccion
    template_name = 'inventario/carrito/test-template.html'
    try:

        items_update = Transaccion.objects.filter(activo=True, carrito_id=pk, herramienta_id=pk)
    except Transaccion.DoesNotExist:
        raise Http404()

    return render(request, template_name, {'items_update':items_update})


class ItemUpdate(UpdateView):
    model = Transaccion
    fields = [
        'carrito',
        'herramienta',
        'cantidad',
        'tipo',
        'motivo',
        'activo',
    ]
    template_name_suffix = '_update_form' 


class ItemDelete(DeleteView):
    model = Transaccion
    success_url = reverse_lazy('item-herramienta')

这些是我对carritos的 models.py

# =========================================================================== #
#   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(default=1)
    activo = models.BooleanField(default=True)
    tipo = models.CharField(max_length=10, choices=CONSUMIBLE, blank=True, null=True)
    motivo = models.CharField(max_length=10, blank=True, null=True)


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

图像#1的模板

{% extends "inventario/base.html" %}


    {% block lista %}
        <div class="container cf">
            <div class="carrito">
                {% if carrito %}
                    {% for c  in carrito %}
                        <div class="side-panel">
                            <div class="info">
                                <p>Carrito:</p> <h1>{{c.no_carrito}}</h1>
                                <p>Asignado a:</p>  <h1>{{c.empleado.nombre}} {{c.empleado.apellido}}</h1>
                            </div>
                            <div class="eliminados">
                                {% if eliminados %}
                                <!-- <table id="tablas" class="cf">
                                    <tr class="theader">
                                        <th>Herramienta da&ntilde;adas</th>
                                        <th>Motivo</th>
                                        <th>Consumible?</th>
                                    </tr>
                                    {% for e in eliminados %}
                                        <tr class="tr-eliminados">
                                            <td>{{e.herramienta}}</td>
                                            <td>{{e.motivo}}</td>
                                            <td>{{e.tipo}}</td>
                                        </tr>
                                    {% endfor%}
                                </table> --> 
                                {% else %}
                                    <h2>No hay elementos eliminados</h2>
                                {% endif %}
                            </div>
                            <a href="" class="btn-carrito dec">Ver reporte de da&ntilde;os</a>

                        </div>

                        <div class="side-main">
                            <table id="tablas" class="cf">
                                <tr class="theader">
                                    <td>Contenido de carrito</td>
                                    <td>cantidad</td>
                                    <td>actualizar</td>
                                </tr>
                                {% for i in trans %}
                                    <tr class="list" >
                                        <td>{{i.herramienta__description}}</td>
                                        <td>{{i.cantidad__sum}}</td>
                                        <td><a href="{% url 'inventario:actualizar-herramienta' pk=c.pk %}" class="btn-lista inc">Actualizar</a></td>       
                                    </tr>
                                {% endfor %}
                            </table>
                            <a href="{% url 'inventario:agregar-herramienta' pk=c.id %}" class="btn-carrito">Agregar</a>
                        </div>
                    {% endfor%}
                {% else %}
                    <h1>No hay articulos por vencer</h1>
                {%endif%}
            </div>
        </div>

    {% endblock lista%}

图像#2的模板

{% extends "inventario/base.html" %}


    {% block lista %}
        <div class="container">
            <table id="tablas" class="cf">
                <tr class="theader">
                    <td>Contenido de carrito</td>
                    <td>cantidad</td>
                    <td>actualizar</td>
                </tr>
                {% for i in items_update %}
                    <tr class="list" >
                        <td>{{i.herramienta}}</td>
                        <td>{{i.cantidad}}</td>
                        <td><a href="#" class="btn-lista dec">Actualizar</a></td>

                    </tr>
                    {% endfor %}
                </table>
        </div>
    {% endblock lista%}

0 个答案:

没有答案