如何从Django的模块中的给定字段中获取所有引用为ForeignKey的对象 [英] How to get all objects referenced as ForeignKey from given field in a module in django

查看:157
本文介绍了如何从Django的模块中的给定字段中获取所有引用为ForeignKey的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班:作者和书。我希望类作者具有一个属性,该属性包含由该作者编写的所有书籍,作为类Books中的外键引用。我采用的方法似乎无效,我认为这是因为在迁移中创建数据库时,还没有Books对象。

  class Author(models.Model):
AuthorName = models.CharField(max_length = 255,unique = True)
books = Book.objects.get(pk = object_instance.pk)

class Book(models.Model):
BookName = models.CharField(max_length = 255)
Author = models.ForeignKey('Author')

我得到的错误消息是:

  NameError:未定义名称'Book'

我得到的是,因为我引用的是另一个类,而实际上并没有该类的实例。我只是不知道执行此操作的正确方法。



编辑:我将其重新格式化为如下格式:

 类Author(models.Model):
AuthorName = models.CharField(max_length = 255,unique = True)
books = author.book_set.all ()

class Book(models.Model):
BookName = models.CharField(max_length = 255)
Author = models.ForeignKey('Author')

这会产生错误:

  NameError:未定义名称作者 

也许我应该只查询数据点



编辑2:答案的解决方案:



所以我一直以来的错误是尝试在author表中添加 books字段。我想那时没有办法了。我可以让该方法在视图中工作,所以我认为这已经解决了,尽管不是我最初计划的方式。



这样做

  class Author(models.Model):
AuthorName = models.CharField(max_length = 255,unique = True)

class Book(models.Model):
BookName = models.CharField(max_length = 255)
Author = models.ForeignKey('Author')

,然后在视图中执行此操作:

  author = Author.objects.get(pk = 1)
books = author.book_get.all()

产生所需的结果(我之前已经知道这一点,但是我试图在模型中实现一个book字段,如果我正确理解的话,至少使用这种方法是不可能的)。 / p>

另一个解决方案:

  class Author(models.Model):
AuthorName = models.CharField(max_length = 255,unique = True)

class Book(mo dels.Model):
BookName = models.CharField(max_length = 255)
Author = models.ForeignKey(Author,related_name = books)


解决方案

您无需在作者模型

  class Author(models.Model):
AuthorName = models.CharField(max_length = 255,unique = True)

class Book(models.Model):
BookName = models.CharField(max_length = 255)
Author = models.ForeignKey('Author')

您可以获取特定作者的所有书籍,例如:

  author = Author.objects.get(id = 1)
图书= author.book_set.all()

了解有关向后关系的更多信息此处


I have two classes: Author and Book. I want class Authors to have an attribute that contains all books written by the said author, as referenced to as foreign key in the class Books. The method I did does not appear to be working, which I assume is because when the database is being created in migrations, no Books objects exist yet. Or so I believe, I'm pretty new at django.

class Author(models.Model):
    AuthorName = models.CharField(max_length=255, unique=True)
    books = Book.objects.get(pk=object_instance.pk)

class Book(models.Model):
    BookName = models.CharField(max_length=255)
    Author = models.ForeignKey('Author')

The error message I get is:

NameError: name 'Book' is not defined

Which I get, is because I'm referencing to another class without actually having and instance of that class. I just can't figure out a proper way to do this.

EDIT: I reformatted it to be like this:

class Author(models.Model):
    AuthorName = models.CharField(max_length=255, unique=True)
    books = author.book_set.all()

class Book(models.Model):
    BookName = models.CharField(max_length=255)
    Author = models.ForeignKey('Author')

which yields error:

NameError: name 'author' is not defined

Maybe I should just query for the datapoints I need later on in views as opposed to creating own field for them in models though..

EDIT 2: solution from answers:

So my mistake all along was to try to add the "books" field in the author table. I guess there's no way to do this then. I can get that method to work in views so I guess this is sort of solved, although not in the way I was originally planning to do it.

doing

class Author(models.Model):
    AuthorName = models.CharField(max_length=255, unique=True)

class Book(models.Model):
    BookName = models.CharField(max_length=255)
    Author = models.ForeignKey('Author')

and then later doing this in views:

author = Author.objects.get(pk=1)
books = author.book_get.all()

yields the wanted result (which I sort of knew beforehand, but I was trying to implement a books field in the models, which, if i correctly understood, is not possible at least not with this method).

another solution:

class Author(models.Model):
    AuthorName = models.CharField(max_length=255, unique=True)

class Book(models.Model):
    BookName = models.CharField(max_length=255)
    Author = models.ForeignKey(Author, related_name = "books")

解决方案

You don't need to create a separate field in Authors model

class Author(models.Model):
    AuthorName = models.CharField(max_length=255, unique=True)

class Book(models.Model):
    BookName = models.CharField(max_length=255)
    Author = models.ForeignKey('Author')

You can get all books of a particular author like:

author = Author.objects.get(id=1)
books = author.book_set.all()

Learn more about backward relationships here

这篇关于如何从Django的模块中的给定字段中获取所有引用为ForeignKey的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆