对象没有属性"entry_set"错误 [英] Object has no attribute 'entry_set' error

查看:107
本文介绍了对象没有属性"entry_set"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,试图通过Python速成课程学习,但遇到了一个我无法解决的问题.这是我的错误:

I'm new to Python, trying to work through Python Crash Course and I've run into a problem that I can't figure out. Here's my error:

比萨"对象没有属性"entry_set"

在我的模型中,我为Toppings中的Pizza定义了外键,并且可以正常工作,但是我显然不理解entry_set定义.

In my model I have the foreign key defined to Pizza in Toppings and it works fine, but I apparently don't understand the entry_set definition.

这是我的网址代码:

from django.urls import path
from . import views

app_name = 'pizzas'

urlpatterns = [
    # home path
    path('', views.index, name='index'),
    # show all pizza names
    path('names/', views.names, name='names'),
    # show all pizza toppings
    path('toppings/', views.toppings, name='toppings'),
    # show a pizza and it's toppings
    path('names/<int:name_id>/', views.pizza, name='pizza'),
]

这是我的查看代码(如您所见,我有entry_set):

Here's my view code (as you can see, I have the entry_set):

def pizza(request, name_id):
    """Show a single pizza and all it's toppings"""
    name = Pizza.objects.get(id=name_id)
    toppings = name.entry_set.order_by('topping')
    context = {'name': name, 'toppings': toppings}
    return render(request, 'pizzas/pizza.html', context)

最后,我的HTML代码:

And lastly, my HTML code:

{% extends 'pizzas/base.html' %}
{% block content %}

  <p>{{ name }}</p>

  <p>Toppings:</p>

    <ul>
      {% for topping in toppings %}
        <li>
            <p>{{ topping|linebreaks }}</p>
        </li>
    </ul>

{% endblock content %}

这是models.py:

Here's the models.py:

from django.db import models


class Pizza(models.Model):
    """Pizza names """
    name = models.CharField(max_length=50)

    def __str__(self):
        """Return a string representation of the model"""
        return self.name


class Toppings(models.Model):
    """Pizza toppings """
    pizza = models.ForeignKey(Pizza, on_delete=models.DO_NOTHING)
    topping = models.CharField(max_length=50)

    def __str__(self):
        """Return a string representation of the model"""
        return self.topping

提前谢谢!

推荐答案

Pizza and Topping模型具有多对一关系.这就是"ForeignKey"关系.数据库中有两个不同的表(还有许多其他表).一份披萨和一份浇头. ForeignKey关系的作用是在"Topping"表中放置一列,指向"Pizza"表中的一行.这是将这些表中的条目相互关联的一种方法.

The Pizza and Topping model have a Many-to-One relationship. That is what a "ForeignKey" relationship is. There are two different tables (among many others) in your database. One for Pizza and one for Toppings. What a ForeignKey relationship does is put a column in your Topping table that points to a row in your Pizza table. It is a way to associate the entries in those tables to each other.

如果您有Topping实例:

If you have Topping instance:

pepperoni.pizza

将退还比萨饼.

您要问的是如何走另一条路.要有一个披萨实例,然后获得浇头.要获得浇头"表中的所有行,并带有一个指向比萨饼"实例的比萨饼"列.

What you're asking about is how to go the other way. To have a pizza instance and then get the toppings. To get all of the rows in your Toppings table with a Pizza column that points to an instance of Pizza.

Django中有一个名为related_name的东西,您可以这样定义它:

There is something in Django called a related_name you can define it as such:

class Toppings(models.Model):
    """Pizza toppings """
    pizza = models.ForeignKey(Pizza, on_delete=models.DO_NOTHING, related_name='toppings')

然后您可以执行以下操作:

with that you can then do this:

afternoon_pizza.toppings.all()

将所有浇头都归还给下午的比萨.默认的related_name是'name_of_your_model'+'_set'.因此,如果不进行上述修改,您可以通过以下方式获得相同的结果:

to return all the toppings on afternoon_pizza. The default related_name is the 'name_of_your_model' + '_set'. So without the above modification you could get the same results with:

afternoon_pizza.toppings_set.all()

我最好的猜测是,您的书中的示例包含一个称为条目"的字段.

My best guess is that the example in your book has a field that is called "entry".

django文档的工作要比我解释的要好得多:

The django docs do a much better job than I do explaining this: https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one/

希望它会有所帮助.

这篇关于对象没有属性"entry_set"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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