1

I have three different models that I want to gather in a feed type page. They do all contain different types of things but for simplicity, the models are the same in this instance.

class ObjectA(models.Model):
    text = models.TextField()
    pub_date = models.DateTimeField('date published',auto_now_add=True)
    ...

class ObjectB(models.Model):
    text = models.TextField()
    pub_date = models.DateTimeField('date published',auto_now_add=True)
    ...

class ObjectC(models.Model):
    text = models.TextField()
    pub_date = models.DateTimeField('date published',auto_now_add=True)
    ...

What would be the general idea to serialize lists of all three objects into one list ordered by pub_date using the Django REST Framework. I just have experience using the meta version below but it can only deal with one model I am assuming. Thanks in advance.

class ObjectAListSerializer(serializers.ModelSerializer):
    class Meta:
        model = ObjectA
        fields = [
            'text',
            'pub_date'
        ]

Pretty much trying to create something that would work like this:

class AllObjectsListSerializer(serializers.ModelSerializer):
5
  • what did you end up doing? Commented Jan 16, 2017 at 23:46
  • So far, I tried a version of the union thing but I'm trying to also experiment around with restructuring my models Commented Jan 17, 2017 at 12:14
  • that last thing is the best option Commented Jan 17, 2017 at 12:15
  • If I do the union then, for something like a feed, would best practices be to have a serialized list of URLs to all the separate instances/models detail API views; then get the JSON from each URL then? Commented Jan 17, 2017 at 12:21
  • pretty hard to say with the information provided. I think you might want to post a new question. Be more detailed Commented Jan 17, 2017 at 12:23

3 Answers 3

2

The most important thing here is that you shouldn't be having three different models here. If they are storing the same data, there should be only one model. To have three models means every time you need to execute a statement you need to precede that with a IF ELIF ELSE which is far from DRY. Not to mention the fact that you need to do a UNION as suggested by Wtower in his answer

Merge the models.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry I should have been more clear on it but the models are different, I just copied and pasted the same one just for this example
Even if the models are similar that means you still have a problem with your table/model design.
0

You need to make a union on the querysets:

query_union = query1 | query 2

And you need a custom serializer for the fields of that union. Or if the union fields are all the same from any of the models, possibly use those model's modelserializer, but haven't tested that one.

Comments

0
dataA = ASerializer(queryset_A, many=True).data
dataB = BSerializer(queryset_B, many=True).data
dataC = CSerializer(queryset_C, many=True).data

just

return Response(data={'dataA ': dataA , 'dataB ': dataB ,'dataC ': dataC })

If you want return a list,the item is {'a':a,''b':'b,'c':c},you should declare you relation between a,b,c and can filter b and c with a.If so,i will write an example for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.