由于某种原因,我收到了一个 AttributeError 。“'动物类型'没有属性'animals_set'”这是它所指的模型:class AnimalType(models.Model): """Type of animal that can classify the animal""" owner = models.ForeignKey(User, on_delete=models.CASCADE) a_type = models.CharField(max_length=50, default='') date_added = models.DateField(auto_now_add=True) def __str__(self): return self.a_typeclass Animal(models.Model): """The actual animal, embeded in animaltype""" animal_type = models.ForeignKey(AnimalType, on_delete=models.CASCADE) name = models.CharField(max_length=60, default='') date_added = models.DateField(auto_now_add=True) def __str__(self): if len(self.name)>20: return self.name[:20] + "..." else: return self.name这是它正在执行的函数:@login_requireddef animal_type(request, animal_type_id): """Shows a single animal type and all of the animals associated""" animal_type = AnimalType.objects.get(id=animal_type_id) #animal type belongs to user if animal_type.owner != request.user: raise Http404 animals = animal_type.animals_set.order_by('-date_added') context = {'animal_type':animal_type, 'animals':animals} return render(request, 'zoo_animal_feeders/animal_type.html', context)
添加回答
举报
0/150
提交
取消