我正在尝试使用Vue和Django,基于ParendID从api中获取子类别
因此,我有两个类别:
例子:
类别:索尼, id: 1
类别:三星, id: 2
因此,当用户点击索尼,id: 1类别(路由器链接),我希望所有基于类别(父)ID的数据都显示在屏幕上(内部组件)。现在发生了什么,当我把ID从父组件传递给子组件时,响应只返回1个对象,它与我从父ID获得的ID相匹配。例如,索尼(父类别)有ID: 1,XZ3(子类别)也有ID: 1,所以它只显示组件内的匹配ID,没有其他
德扬戈
views.py:
from rest_framework import serializers
from . models import Specs, Models, Brands
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from . serializers import ModelsSerializer, SpecsSerializer, BrandsSerializer
# Create your views here.
@api_view(['GET'])
def BrandsView(request):
brand = Brands.objects.all()
serializer = BrandsSerializer(brand, many=True)
return Response(serializer.data)
@api_view(['GET'])
def ModelsView(request, pk):
model = Models.objects.get(pk=pk)
serializer = ModelsSerializer(model, many=False)
return Response(serializer.data)
@api_view(['GET'])
def SpecsView(request, pk):
specs = Specs.objects.get(pk=pk)
serializer = SpecsSerializer(specs, many=False)
return Response(serializer.data)
网址。py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.BrandsView),
path('models/<pk>/', views.ModelsView),
path('specs/<pk>/', views.SpecsView)
]
models.py
from django.db import models
from django.db.models.deletion import CASCADE
from django.urls import base, reverse
# from django_resized import ResizedImageField
from django.utils import timezone
# Create your models here.
class Brands(models.Model):
brand = models.CharField(max_length=20)
def __str__(self):
return(f'{ self.brand }')
class Models(models.Model):
brand = models.ForeignKey('Brands', on_delete=models.CASCADE, null=True)
model = models.CharField(max_length=100)
createdOn = models.DateField(default=timezone.now)
warranty = models.BooleanField()
damaged = models.BooleanField()
repaired = models.BooleanField()
firstOwner = models.BooleanField()
price = models.IntegerField()
def __str__(self):
return(f'{ self.brand } - { self.model } / { self.price }e / Created: { self.createdOn }')
class Specs(models.Model):
model = models.ForeignKey('Models', on_delete=models.CASCADE, null=True)
dimensions = models.CharField(max_length=50)
weight = models.CharField(max_length=10)
screen = models.CharField(max_length=200)
cpu = models.CharField(max_length=100)
gpu = models.CharField(max_length=100)
mainCam = models.CharField(max_length=50)
selfieCam = models.CharField(max_length=50)
video = models.CharField(max_length=100)
battery = models.CharField(max_length=10)
fastCharging = models.BooleanField()
def __str__(self):
return(f'{ self.model }')
序列化程序。派克
from django.db import models
from django.db.models import fields
from . models import Specs, Models, Brands
from rest_framework import serializers
class BrandsSerializer(serializers.ModelSerializer):
class Meta:
model = Brands
fields = '__all__'
class ModelsSerializer(serializers.ModelSerializer):
class Meta:
model = Models
fields = '__all__'
class SpecsSerializer(serializers.ModelSerializer):
class Meta:
model = Specs
fields = '__all__'
VUE
index.js
import { createRouter, createWebHistory } from 'vue-router'
import About from '../views/About.vue'
import Models from '../views/Models.vue'
import Specs from '../views/Specs.vue'
import Brands from '../views/Brands.vue'
const routes = [
{
path: '/',
name: 'Brands',
component: Brands
},
{
path: '/models/:id',
name: 'Models',
component: Models,
props: true
},
{
path: '/specs/:id',
name: 'Specs',
component: Specs,
props: true
},
{
path: '/about',
name: 'About',
component: About
},
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
})
export default router
品牌。vue
<template>
<div>
<h1> Brands </h1>
<div v-for="brand in brands" v-bind:key="brand.brand">
<router-link :to="{ name: 'Models', params: {id: brand.id} }">
{{ brand }}
</router-link>
</div>
</div>
</template>
模型。vue
<template>
<div>
<h1> Models </h1>
<div v-if="models">
<hr>
<table>
<tr>
<th> Model </th>
<th> Created </th>
<th> Warranty </th>
<th> Damaged </th>
<th> Repaired </th>
<th> First Owner </th>
<th> Price </th>
<!-- <th> More Specs </th> -->
</tr>
<tr>
<td> {{ models.model }} </td>
<td> {{ models.createdOn }} </td>
<td> {{ models.warranty }} </td>
<td> {{ models.damaged }} </td>
<td> {{ models.repaired }} </td>
<td> {{ models.firstOwner }} </td>
<td> {{ models.price }}e </td>
<th>
<!-- <router-link :to="{ name: 'Specs', params: {id: model.id} }">
HERE
</router-link> -->
</th>
</tr>
</table>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
props: ['id'],
// template: ['id'],
data(){
return {
models: []
}
},
mounted(){
axios.get('http://localhost:8000/models/' + this.id)
.then(response => {
this.models = response.data
})
}
}
</script>
<style scoped>
.models{
background-color: lightcyan;
border: 10px;
}
</style>
规格vue
<template>
<div v-if="specs" class="specs">
<ul>
<li> <b>Model:</b> {{ specs.model }} </li> <br>
<li> <b>Dimensions:</b> {{ specs.dimensions }} </li> <br>
<li> <b>Weight:</b> {{ specs.weight }} </li> <br>
<li> <b>Screen:</b> {{ specs.screen }} </li> <br>
<li> <b>Cpu:</b> {{ specs.cpu }} </li> <br>
<li> <b>Gpu:</b> {{ specs.gpu }} </li> <br>
<li> <b>Main Camera:</b> {{ specs.mainCam }} </li> <br>
<li> <b>Front Camera:</b> {{ specs.selfieCam }} </li> <br>
<li> <b>Video:</b> {{ specs.video }} </li> <br>
<li> <b>Battery:</b> {{ specs.battery }} </li> <br>
<li> <b>Fast Charging:</b> {{ specs.fastCharging }} </li> <br>
</ul>
<hr>
<router-link to="/"> Back to HOME </router-link>
</div>
</template>
<script>
import axios from 'axios'
export default {
props: ['id'],
data(){
return {
specs: [],
}
},
mounted(){
axios.get('http://localhost:8000/specs/' + this.id)
.then(response => {
this.specs = response.data
})
}
}
</script>
<style scoped>
.specs ul li {
list-style-type: none;
float: left;
}
/* .th {
border: 1px solid;
border-radius: 10px;
padding: 20px;
margin: 10% auto;
box-shadow: 5px 5px 10px 5px ;
background-color: aliceblue;
} */
.specs {
background-color: lightcyan;
border: 10px;
padding: 10px;
}
</style>
您需要在品牌模型和模型之间创建嵌套序列化程序。因此,您需要将以下序列化程序类添加到序列化程序中。py,查看您的视图。py,以及url的url路径。皮耶。当然,您可以随意命名类、函数和url路由。另外,我建议您阅读DRF文档中的“嵌套序列化”一章。
// serializers.py
class Brands_ModelsSerializer(serializers.ModelSerializer):
models = ModelsSerializer(source="models_set", many=True)
class Meta:
model = Brands
fields = ['models']
// views.py
@api_view(['GET'])
def brand_with_models(request, pk):
brand_models = Brands.objects.get(pk=pk)
serializer = Brands_ModelsSerializer(brand_models)
return Response(serializer.data)
// urls.py
path('brand_models/<pk>/', views.brand_with_models)
有一个类别,具有以下(重要)属性: 我正在尝试实现类别/子类别的概念。一个类别可以有零个或多个子类别。一个类别只能有一个父类别(可以为null)。对于顶级类别,parentCategory=null。 现在,我有一个所有类别的列表,
问题内容: 这是表结构 我如何将表格显示为 上表显示升序ID,后跟与该ID相关联的parent_id,然后转到第二个ID,依此类推。我需要在单个查询中,是否可以这样做? 问题答案: 试试这个: http://sqlfiddle.com/#!2/9b05f/3 编辑: 鉴于这个答案一直在不断增加,我重新考虑了这个问题并发现了一个缺陷。如果由于某种原因,父级的ID高于子级的ID,则排序会混乱。仅当
我使用codeigniter和有一个表3列(id,名称,parent_id)。一个类别可以有多个子类别,一个子类别可以有多个子类别。 我一直在尝试使用以下代码获取所有类别及其子类别: 但它只返回1级类别。 我的问题是如何完成我的任务:获得每个级别的所有类别和子类别。
我试图在WooCommerce的子类别页面中查找父类别名称,即我有4个主要类别。 父母1 子1 子2 子3等 父母2 子1 子2 子3等 等等。 如果我在Sub2的列表页上,Sub2是Parent1的子级,我想知道Parent1类别的名称。
试图弄清楚你将如何着手检查Woocommerce类别是否为父类别。 我有一个类别和一个子类别,在我的Woocommerce模板中,有两个单独的布局HTML。父类别为1,子类别为1。 我目前正在使用这个: 但是,如果它可以在不手动输入所有父类别的情况下进行检查,则会更容易。 还有,有没有办法从子类别或子类别产品页面链接到主父类别页面?
我想在前端获得所有woocommerce类别的子类别,结果如下: 这是我所拥有的(但不是我想要的): 这段代码显示了类别和子类别,但是子类别不在应该在的地方,子类别就像这样的单独链接: