返回

python-Django-如何从关系中获取数据?

发布时间:2022-06-18 07:49:41 304
# node.js

我尝试通过多对多和一对多关系获取数据。

view.py

class PublisherDetailView(generic.DetailView):

     model = Publisher

     template_name = 'store/publisher_detail_view.html'

models.py

class Publisher(models.Model):

    name = models.CharField(null=False, max_length=30)

    image = models.ImageField(upload_to='publisher_images/')

class Book(models.Model):

    title = models.CharField(null=False, max_length=30)

    description = models.CharField(max_length=1000)

    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE) 

class reader(models.Model):

    name = models.CharField(max_length=40)

    book = models.ManyToManyField(Book)

publisher_detail_view.html

{% for reader in publisher.book_set.readers.all %}

    <p>{{ reader.name }} </p>

{% endfor %}

我只想从特定的出版商那里获得所有读者。模板中的正确代码是什么?publisher.book_set.readers.all对我来说有意义(早期初学者),但不起作用

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像