Получение данных о пользователе из другой модели
У меня есть модель (Poll), которая содержит другую модель (User) с данными пользователя. Я хочу получить имя пользователя, опубликовавшего опрос
но ниже произошла ошибка
I/flutter (21892): type 'int' is not a subtype of type 'String'
это мой класс модели (flutter)
вот как я разбираю json
List<Poll> _polls = [];
Future<bool> getOffers() async {
var url = Uri.parse(polls_uri);
try {
http.Response response = await http.get(url);
var data = json.decode(response.body);
List<Poll> temp = [];
for (var element in data) {
Poll poll = Poll.fromJson(element);
temp.add(poll);
}
_polls = temp;
return true;
} catch (e) {
print(e);
return false;
}
}
List<Poll> get polls {
return [..._polls];
}
здесь я хочу отобразить детали
ListView.builder(
itemCount: polls.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(polls[index].question),
Text(polls[index].createdBy.username),
]),
),
);
},
),
класс моей модели (django)
class Poll(models.Model):
question = models.CharField(max_length=100)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
pub_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.question
serializer
class PollSerializer(serializers.ModelSerializer):
choices = ChoiceSerializer(many=True, read_only=True, required = False)
class Meta:
model = Poll
fields = '__all__'
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
просмотров
class PollList(generics.ListCreateAPIView):
queryset = Poll.objects.all() [:20]
serializer_class = PollSerializer