I am building an API from a model which gives me the following structure

{ 
  "user_nucleus_id": 2, 
  "user_id": 2, 
  "nucleus": 2, 
  "family_id": 2, 
  "date": "1974-05-19T19:00:00-05:00" 
}

But now I get the following API structure request. What I understand is 
that I must serialize the same model twice so that in the "nucleus_users" 
array it brings me the user id and the family id of the same model.

{
    "user_nucleus_id": 2,
    "user_id": 2,
    "nucleus": 2, 
    "family_id": 2,
    "date": "1974-05-19T19:00:00-05:00", 
    "nucleus_users":[ 
         { 
                   "user_id":"",
                   "family_id":"",
          }
 ], 
}

I would like to know if you can guide me in this special case of how to 
build this API with this structure from a single model.

Thank you...

models.py
class Usernucleus(models.Model):
user_nucleus_id = models.BigAutoField(primary_key=True)
nucleus = models.ForeignKey(Nucleofamiliar ,on_delete=models.PROTECT)
user = models.ForeignKey(Usuario, on_delete=models.PROTECT)
family = models.ForeignKey(Parentesco, on_delete=models.PROTECT)
date = models.DateTimeField()

serializer.py
class UsernucleusSerializers(serializers.ModelSerializer):

class Meta:
model = Usernucleus
fields = ['user_nucleus_id','user','family','date']

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/97b8b922-3b99-4d8b-9ff7-7648d6f217e2n%40googlegroups.com.

Reply via email to