Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django3.2中如何通过ForeignKey获取对象_Python_Django_Foreign Keys - Fatal编程技术网

Python 在Django3.2中如何通过ForeignKey获取对象

Python 在Django3.2中如何通过ForeignKey获取对象,python,django,foreign-keys,Python,Django,Foreign Keys,我正在为Irvine类构建菜单项,并希望按类别对它们进行分类 models.py class Irvine(models.Model): objects = None name = models.CharField(max_length=50, verbose_name='Irvine Item') description = models.TextField(null=True, blank=True) size = models.FloatField(null

我正在为
Irvine
类构建菜单项,并希望按
类别对它们进行分类

models.py

class Irvine(models.Model):
    objects = None
    name = models.CharField(max_length=50, verbose_name='Irvine Item')
    description = models.TextField(null=True, blank=True)
    size = models.FloatField(null=True, blank=True)
    price = models.FloatField(null=True, blank=True)
    published = models.DateTimeField(auto_now_add=True, db_index=True)

    category = models.ForeignKey('Category', null=True, on_delete=models.PROTECT, verbose_name='Category')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'Irvine'
        verbose_name = 'Irvine Item'
        ordering = ['-published']


class Category(models.Model):
    objects = None
    name = models.CharField(max_length=30, db_index=True, verbose_name="Category")
    published = models.DateTimeField(auto_now_add=True, db_index=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = '* Categories'
        verbose_name = 'Category'
        ordering = ['name']
view.py

def irvine(request):
    irvine = Irvine.objects.all()
    context = {'irvine': irvine}
    return render(request, 'cafe/irvine.html', context)

def by_category(request, category_id):
    santaanna = SantaAnna.objects.filter(category=category_id)
    costamesa = CostaMesa.objects.filter(category=category_id)
    irvine = Irvine.objects.filter(category=category_id)
    categories = Category.objects.all()
    current_category = Category.objects.get(pk=category_id)
    context = {'santaanna': santaanna, 'categories': categories, 'costamesa': costamesa, 'irvine': irvine, 'current_category': current_category}

    return render(request, 'cafe/by_category.html', context)
url.py

urlpatterns = [
    path('add/', ItemsCreateView.as_view(), name='add'),
    path('<int:category_id>/', by_category, name='by_category'),
    path('', index, name='index'),
    path('irvine', irvine),

urlpatterns=[
路径('add/',ItemsCreateView.as_view(),name='add'),
路径(“/”,按类别,名称='按类别〕,
路径(“”,索引,name='index'),
小径(‘欧文’、欧文),

{%for i in irvine%}
{}
{{i.name}
{{i.description}}
{{i.size}}
{{i.price}}
{%endfor%}

我可以从Irvine类中获取所有项目,但是如何按类别从该类中获取项目

您不能直接使用
I.category
进行检查,因为它有一个值列表

尝试使用
i.category.name

如果您有序列化程序,请更新完整代码


{%for i in irvine%}{%i.category.name=='开胃菜'%}
,它将起作用

irvine拥有category(ForeignKey)的对象,尝试在by_category方法中打印irvine并共享结果。我对Django和python是新手,因此我不知道应该打印什么,如果您可以指定的话,我会非常失望。@Mythilydevaraj好的,我知道了,在html中使用i.category时,它显示了正确的类别`{%for i in irvine%}{%if i.category==开胃菜%}`ok''{%for i in irvine%}{%if i.category=='开胃菜'%}''不起作用,但是如果我做了{%if i.name=='土豆'%}现有项目,它会显示该项目。为什么我看不到i.categories?是的,它起作用了!非常感谢您的帮助!!!
  {% for i in irvine %}
    {}
    <tr class="danger">

      <th scope="row" width="20%">{{ i.name }}</th>
      <td width="60%">{{ i.description }}</td>
      <td width="10%">{{ i.size }}</td>
      <td width="10%">{{ i.price }}</td>

    </tr>
  {% endfor %}