Это код для получения всех подписчиков для указанного c идентификатора, затем передачи этих идентификаторов, чтобы затем получить все свои сообщения / действия из другой таблицы. Но я не могу этого понять.
@api_view(['GET'])
def build_ecosystem(request, id):
"""
1. first get all objects that follow the given id
2. find all the activities posted by all those ids
3. send the payload
"""
list_of_followers,ecosystem_activities = [], []
def get_object(id):
try:
return startup_activities.objects.filter(stup=id)
except startup_activities.DoesNotExist:
raise Http404
def get_joined_eco_objs(id):
try:
return follows.objects.filter(ceo_id__exact=id)
except startup_activities.DoesNotExist:
raise Http404
if request.method == 'GET':
sd = get_joined_eco_objs(id)
serializer = FollowerFollowingSerializer(sd, many = True)
the_list = serializer.data
for an_item in the_list:
list_of_followers.append(an_item["stup_follows"])
for x in range(len(list_of_followers)):
ecosystem_activities = StupActivitiesSerializer(startup_activities.objects.filter(stup=list_of_followers[x],many = True))
return Response(ecosystem_activities.data)
Но когда я передаю идентификатор, я получаю: []
РЕДАКТИРОВАТЬ: БОЛЬШЕ ДЕТАЛЕЙ: МОДЕЛИ:
class startup_activities(models.Model):
stup = models.ForeignKey(startup_details, on_delete=models.CASCADE)
text = models.CharField(max_length=250)
links = models.CharField(max_length=800)
time = models.DateTimeField(default=timezone.now)
title = models.CharField(max_length=200)
class follows(models.Model):
ceo_id = models.ForeignKey(startup_details, on_delete=models.CASCADE)
stup_follows = models.IntegerField(default=0)
class startup_details(models.Model):
ceo_name = models.CharField(max_length=200)
startup_name = models.CharField(max_length=200)
startup_about = models.TextField()
date_founded = models.DateTimeField(default=timezone.now)
def founded_date(self):
self.date_founded = timezone.now()
self.save()
СЕРИАЛИЗАТОР:
class StupActivitiesSerializer(serializers.ModelSerializer):
class Meta:
model = startup_activities
fields = ('stup', 'text', 'links', 'time', 'title')
ОКОНЧАНИЕ МЕТОДА:
path('get_activities_by_ecofollowers/<int:id>/', views.build_ecosystem, name='build_ecosystem'),