Django REST框架和组合模型 [英] Django REST Framework and combining models

查看:30
本文介绍了Django REST框架和组合模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新的问题

我能够使其正常运行,并更新了以下代码.但是现在我的问题是如何编辑 iceinfo 中的字段?现在,它以 IceInfo对象的形式返回,而不是以可编辑字段的形式返回.我只能编辑 ice_code ice_maker .

I was able to get it to work and have updated the codes below. But now my question is how can the fields in iceinfo be edited? As it stands now it is returned as IceInfo object and not as editable fields. I can edit ice_code and ice_maker only.

旧问题

我正在尝试为我们的数据库构建一个API.数据库中的信息分为多个表,所有表的主键具有相同的" Ice-code ".

I'm trying to build an API for our database. The information in the database is divided between multiple tables, all of which have the same 'Ice-code' as their primary key.

我现在已经尝试了一周的大部分时间来合并表格,这样我就可以从一个网址( api.something.com/ice/)看到列表ice,并从( api.something.com/ice/1 )可以查看结合了所有表中信息的ice的详细视图.

I've been trying now for a better part of a week to combine the tables so I can from one url (api.something.com/ice/) be able to see a list of ice and from (api.something.com/ice/1) be able to see a detailed view of the ice combining information from all tables.

看来,无论我怎么做,我都无法合并表格.

Seems that no matter what I try I fail to combine the tables.

这是我到目前为止的内容的粗略草稿.我希望最终能够从前端更新字段.每张表的总行数大约为70-80k,并且它们的列数比这里的要多,但我只是想做一个粗略的工作草案.

Here is a rough draft of what I have so far. I would like to be able to update the fields from the front end eventually. Total number of rows per table is somewhere around 70-80k and they have more columns than the ones here but I'm just trying to make a rough working draft.

#models.py

class IceInfo(models.Model):

    ice_name = models.TextField(db_column='Ice name', blank=True, null=True)
    updated = models.DateTimeField(db_column='Updated')
    ice_code = models.ForeignKey(IceList, related_name='iceinfo', db_column='Ice-code', on_delete=models.CASCADE, primary_key=True)

    class Meta:
        managed = False
        db_table = 'Ice_Info'


class IceList(models.Model):
    ice_code = models.IntegerField(primary_key=True, db_column='Ice-code', max_length=10)
    ice_maker = models.CharField(db_column='Ice Maker', max_length=255, blank=True, null=True)
    updated = models.DateTimeField(db_column='Updated')
    class Meta:
        managed = False
        db_table = 'Ice_List'

    def __str__(self):
        return self.ice_code

#serializers.py

from rest_framework import serializers
from .models import IceList, IceInfo



class IceInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = IceInfo
        fields = '__all__'


class IceListSerializer(serializers.ModelSerializer):

    iceinfo = IceInfoSerializer(many=True)

    class Meta:
        model = IceList
        fields = ('ice_code', 'ice_maker', 'iceinfo')

#views.py

from .models import IceInfo, IceList
from .serializers import IceListSerializer
from rest_framework.generics import ListAPIView, RetrieveAPIView, RetrieveUpdateAPIView
from rest_framework.filters import SearchFilter, OrderingFilter


class IceList(ListAPIView):
    queryset = IceList.objects.all()
    serializer_class = IceListSerializer
    filter_backends = [SearchFilter, OrderingFilter]
    search_fields = ['ice_code', 'ice_maker', 'ice_name']

结果

"results": [
        {
        "ice_code": 1,
        "iceinfo": [
        {
            ice_name": "Ice Name 1"
        }
        )
        "ice_maker": "Ice Maker 1"
        },
       {
        "ice_code": 2,
        "iceinfo": [
        {
            ice_name": "Ice Name 2"
        }
        )
        "ice_maker": "Ice Maker 2"

        },

推荐答案

假设您的目标是序列化许多IceInfo实例,那么IceList的目的是什么?还是您尝试不同的东西?Rest框架将多个对象序列化为列表没有问题.

Assuming your goal is to serializers many instances of IceInfo, what is the purpose of IceList? Or are you trying something different? Rest framework has no problem serialising multiple objects into a list.

您可以通过将queryset传递给序列化器,然后给关键字关键字many = True来实现.看这里:文档

You can do that by passing the queryset to the serializer, and give give the keyword many=True. See here: documentation

结果将是类似 queryset = IceInfoSerializer(IceInfo.objects.all(),many = True).

关于新问题,您应该可以从查询集中对其进行编辑.你都尝试了些什么?(您最好在新问题上打开一个新主题)

About the new question, you should be able to edit them from the queryset. What have you tried so far? (You may be better of opening a new topic on the new question)

此外,您发布的JSON似乎无效,您可以检查复制是否正确吗?

Also, the JSON you posted doesn't seem valid, can you check if you copy pasted it correctly?

这篇关于Django REST框架和组合模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆