type d'objet " X "n'a pas d'attribut "objets"
Je suis à l'aide de Django Django et de Repos Cadre 2.4.0
- Je obtenir de l'Attribut erreur type object 'Notification' has no attribute 'objects'
models.py
class Notification(models.Model):
NOTIFICATION_ID = models.AutoField(primary_key=True)
user = models.ForeignKey(User, related_name='user_notification')
type = models.ForeignKey(NotificationType)
join_code = models.CharField(max_length=10, blank=True)
requested_userid = models.CharField(max_length=25, blank=True)
datetime_of_notification = models.DateTimeField()
is_active = models.BooleanField(default=True)
serializers.py:
class NotificationSerializer(serializers.ModelSerializer):
class Meta:
model = Notification
fields = (
'type',
'join_code',
'requested_userid',
'datetime_of_notification'
)
api.py:
class Notification(generics.ListAPIView):
serializer_class = NotificationSerializer
def get_queryset(self):
notifications = Notification.objects.all()
return notifications
Quelqu'un peut-il m'aider à comprendre cela? Il échoue dans api.py
à la ligne notifications = Notification.objects.all()
OriginalL'auteur Matchday | 2016-02-21
Vous devez vous connecter pour publier un commentaire.
La ligne
notifications = Notification.objects.all()
référence à laNotification
classe définie dans api.py et pas models.py.La façon la plus simple pour corriger cette erreur consiste à renommer le
Notification
classe dans api.py ou models.py de sorte que vous pouvez vous référer à votre modèle correctement. Une autre option serait d'utiliser nommé importations:OriginalL'auteur Derek Kwok
Ajouter
objects = models.Manager()
à votre modèle, ou tout autre gestionnaire que vous utilisez.OriginalL'auteur Paul Tuckett