问题
Updated question
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.
Old question
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.
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.
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 in
"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"
},
回答1:
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.
You can do that by passing the queryset to the serializer, and give give the keyword many=True. See here: documentation
The result would be something like
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)
Also, the JSON you posted doesn't seem valid, can you check if you copy pasted it correctly?
来源:https://stackoverflow.com/questions/42611225/django-rest-framework-and-combining-models