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
Django 检索多对多关系上的ImageField_Django - Fatal编程技术网

Django 检索多对多关系上的ImageField

Django 检索多对多关系上的ImageField,django,Django,我有两个与多对多关系相关的应用程序,菜谱和标签,我在检索带有图像的标签查询时遇到问题!=null和当前发布的配方 配方应用程序:recipes/models.py from django.db import models from django.contrib.auth.models import User from tags.models import Tag DRAFT = 'D' PUBLISHED = 'P' RECIPE_STATES = ( (DRAFT, 'Draft')

我有两个与多对多关系相关的应用程序,菜谱和标签,我在检索带有图像的标签查询时遇到问题!=null和当前发布的配方

配方应用程序:recipes/models.py

from django.db import models
from django.contrib.auth.models import User
from tags.models import Tag

DRAFT = 'D'
PUBLISHED = 'P'
RECIPE_STATES = (
    (DRAFT, 'Draft'),
    (PUBLISHED, 'Published')
)

class Recipe(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=100, unique=True)
    date_created = models.DateField(auto_now_add=True)
    date_modified = models.DateField(auto_now=True)
    state = models.CharField(max_length=1, choices=RECIPE_STATES)
    ingredients = models.TextField(blank=True)
    introduction = models.TextField(blank=True)
    preparation = models.TextField(blank=True)
    tip = models.TextField(blank=True)
    rating = models.IntegerField(blank=True)
    diners = models.IntegerField(blank=True)
    tags = models.ManyToManyField(Tag, blank=True)

    def __str__(self):
        return self.title
from django.db import models

class Tag(models.Model):
    name = models.CharField(max_length=50, unique=True)
    image = models.ImageField(blank=True, null=True, upload_to='categories')

    def __str__(self):
        return self.name
标签应用程序:tags/models.py

from django.db import models
from django.contrib.auth.models import User
from tags.models import Tag

DRAFT = 'D'
PUBLISHED = 'P'
RECIPE_STATES = (
    (DRAFT, 'Draft'),
    (PUBLISHED, 'Published')
)

class Recipe(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=100, unique=True)
    date_created = models.DateField(auto_now_add=True)
    date_modified = models.DateField(auto_now=True)
    state = models.CharField(max_length=1, choices=RECIPE_STATES)
    ingredients = models.TextField(blank=True)
    introduction = models.TextField(blank=True)
    preparation = models.TextField(blank=True)
    tip = models.TextField(blank=True)
    rating = models.IntegerField(blank=True)
    diners = models.IntegerField(blank=True)
    tags = models.ManyToManyField(Tag, blank=True)

    def __str__(self):
        return self.title
from django.db import models

class Tag(models.Model):
    name = models.CharField(max_length=50, unique=True)
    image = models.ImageField(blank=True, null=True, upload_to='categories')

    def __str__(self):
        return self.name
recipes/views.py:

class HomeView(View):
    def get(self, request):
        queryset = Recipe.objects.filter(state=PUBLISHED)
        last_recipes = queryset.order_by('-date_created')
        top_recipes = queryset.order_by('-rating')
        categories = Tag.objects.filter(image__isnull=False, recipe__state=PUBLISHED)
        context = {
            'last_recipes': last_recipes[:4],
            'top_recipes': top_recipes[:4],
            'categories': categories
        }
        return render(request, 'recipes/home.html', context)
当我尝试从home.html中的views.py检索该查询时:

<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12">
        {% for category in categories %}
            <a href="{% url 'category_recipes' category.name %}">
            <div class="col-xs-6 col-sm-4 col-md-3 text-center">
                <h3>{{ category.name }}</h3>
                <img src="{{ category.image.url }}" alt="{{ category.name }}" height="100">
            </div>
            </a>
        {% endfor %}
    </div>
</div>
我还用数据填充了标签:

检查,id=20的标记没有“”作为值(空字符串)。您只排除空值,但不检查空字符串。

我最终使用以下方法使其工作:

categories = Tag.objects.filter(image__isnull=False, recipe__state=PUBLISHED).distinct()
现在,home.html中的categories提供了数据,我在使用category.image.url时没有遇到问题