【问题标题】:Retrieve ImageField on Many-To-Many relationship检索多对多关系上的 ImageField
【发布时间】:2017-11-27 07:46:07
【问题描述】:

我有两个以多对多关系关联的应用程序,即食谱和标签,我在检索具有 image != 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

还有标签应用:tags/models.py

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

食谱/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>

我收到此错误:

ValueError at /
The 'image' attribute has no file associated with it.

我还用数据填充了标签:

【问题讨论】:

    标签: django


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我终于让它工作了:

      categories = Tag.objects.filter(image__isnull=False, recipe__state=PUBLISHED).distinct()
      

      现在,home.html 中的类别已提供数据,我在使用 category.image.url 时没有遇到问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-10
        • 2017-05-25
        • 2019-08-17
        • 2017-01-17
        • 2023-03-12
        • 1970-01-01
        • 2015-03-06
        相关资源
        最近更新 更多