Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

python - QuerSet object has no attribute

i am trying to create a "Kriskras" that has a list of activities behind it. so i used a foreign key to link them. but every time i am trying to add something to the list i get the error

AttributeError: 'QuerySet' object has no attribute 'vergadering_set'

this is my model

from django.db import models

# Create your models here.

class Kriskras(models.Model):
    tak = models.CharField(max_length=20)


    def __str__(self):
        return self.tak

class Vergadering(models.Model):
    kriskras = models.ForeignKey(Kriskras, on_delete=models.CASCADE)
    date = models.CharField(max_length=50)
    extra = models.CharField(max_length=5)
    activiteit = models.CharField(max_length=500)

    def __str__(self):
        return self.activiteit
    


how can i solve it so i can use the vergadering_set attribute?

question from:https://stackoverflow.com/questions/65952251/querset-object-has-no-attribute

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason is you are using a reverse relation with all objects which is not allowed. You can use vergadering_set like this:

# pay attention I'm not using Kriskras.objects.all()
ls = Kriskras.objects.first() # will return first item from database.
ls.vergadering_set.all() # will return the expected result

You can read more about this here in django's docs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...