我有一个模型,其中Vehicle
表具有更多Wheels
表。我试图在单个字段中显示车辆和相关“车轮”表中第一个车轮的信息。我已经看到F
函数可能有用,但是我找不到正确的配置才能正常工作。这些表通过另一个名为colour
的字段关联,该字段在Wheels
表中声明为外键。
class VehicleListView(ListView):
template_name = 'vehicle.html'
queryset = Vehicle.objects.all()
queryset = queryset.annotate(wheel1_name = F('Wheels__wheel_name'))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
我想要这行
queryset = queryset.annotate(wheel1_name = F('Wheels__wheel_name'))
返回每个车辆的第一个车轮名称的列表,以便我可以对其进行迭代并将其显示在表格中。
models.py
class Vehicle(models.Model):
vehicle_id = models.AutoField(primary_key=True)
vehicle_name = models.CharField(max_length=100)
color = models.CharField(max_length=100)
class Wheels(models.Model):
wheels_id = models.AutoField(primary_key=True)
color = models.ForeignKey(Vehicle, null=True, on_delete=models.SET_NULL,
db_column='color')
wheel_name = models.CharField(max_length=100)
答案 0 :(得分:1)
所以我稍微更改了代码,设置了数据库,创建了2个Vehicle
对象,每个对象有2个相关的Wheels
对象,所以总共有4个Wheels
对象。
我为某个特定的Wheel
对象的第一个相关Vehicle
对象添加了queries的函数。
# models.py
class Vehicle(models.Model):
vehicle_id = models.AutoField(primary_key=True)
vehicle_name = models.CharField(max_length=100)
color = models.CharField(max_length=100)
# this function returns a slice of the resulting queryset which contains
# the first related Wheels object for each Vehicle object
def get_first_wheels(self):
return Wheels.objects.filter(color=self).order_by('wheels_id')[:1]
class Wheels(models.Model):
wheels_id = models.AutoField(primary_key=True)
color = models.ForeignKey(Vehicle, null=True, on_delete=models.SET_NULL)
wheel_name = models.CharField(max_length=100)
我使用Django serializers来序列化数据。为了获得第一个相关的Wheels
对象,我使用了SerializerMethodField()
,它在get_first_wheels()
中调用models.py
函数,如下所示:
# serializers.py
class WheelsSerializer(serializers.ModelSerializer):
class Meta:
model = Wheels
fields = ('wheels_id', 'color', 'wheel_name')
class VehicleSerializer(serializers.ModelSerializer):
first_wheel = serializers.SerializerMethodField(read_only=True)
def get_first_wheel(self, model):
qs = model.get_first_wheels()
return WheelsSerializer(qs, many=True).data
class Meta:
model = Vehicle
fields = ('vehicle_id', 'vehicle_name', 'color', 'first_wheel')
我稍微改变了您的看法,改用ModelViewSet
。
# views.py
class VehicleViewSet(viewsets.ModelViewSet):
serializer_class = VehicleSerializer
queryset = Vehicle.objects.all()
我去了DefaultRouter()
,并用这种方式注册了端点:
# urls.py
from rest_framework import routers
# import ViewSet here
router = routers.DefaultRouter()
router.register(r'vehicles`, views.VehicleViewSet, base_name='vehicle')
然后我运行了以下命令:
manage.py makemigrations
manage.py migrate
manage.py runserver
我创建了2个Vehicle
对象,每2个相关的Wheels
对象。
当我在浏览器中转到http://127.0.0.1:8000/vehicles/
时,它将返回所有Vehicle
对象以及它们的第一个相关Wheels
对象,如下所示:
[
{
"vehicle_id": 1,
"vehicle_name": "BMW",
"color": "Blue",
"first_wheel": [
{
"wheels_id": 1,
"color": 1,
"wheel_name": "BMW1"
}
]
},
{
"vehicle_id": 2,
"vehicle_name": "Ferrari",
"color": "Red",
"first_wheel": [
{
"wheels_id": 3,
"color": 2,
"wheel_name": "Ferrari1"
}
]
}
]