获取/呈现一对多关系的最佳方法是什么?

时间:2021-12-19 20:49:36

I have 2 models which look like that:

我有两个看起来像这样的模型:

class Entry(models.Model):
user = models.ForeignKey(User)
dataname = models.TextField()
datadesc = models.TextField()
timestamp = models.DateTimeField(auto_now=True)

class EntryFile(models.Model):
    entry = models.ForeignKey(Entry)
    datafile = models.FileField(upload_to="uploads/%Y/%m/%d/%H-%M-%S")

I want to render all the entries with their related files for a specific user. Now I am doing it that way in my view to get the values:

我想为特定用户呈现所有条目及其相关文件。现在我在我的视图中这样做以获取值:

    entries = Entry.objects.filter(user=request.user).order_by("-timestamp")
    files = {}
    for entry in entries:
        entryfiles = EntryFile.objects.filter(entry=entry)
        files[entry] = entryfiles
    return render_to_response("index.html", {'user': request.user, 'entries': entries, 'files': files, 'message': message})

But I am not able/don't know how to work with these data in my template. This what I do now, but isn't working:

但我无法/不知道如何在我的模板中使用这些数据。这就是我现在所做的,但是不起作用:

{% for entry in entries %}
    <td>{{ entry.datadesc }}</td>
    <td><table>
        {{ files.entry }}
        {% for file in files.entry %}
        <td>{{ file.datafile.name|split:"/"|last }}</td>
        <td>{{ file.datafile.size|filesizeformat }}</td>
        <td><a href="{{ object.datafile.url }}">download</a></td>
        <td><a href="{% url main.views.delete object.id %}">delete</a></td>
        {% endfor %}
    </table></td>
{% endfor %}

Anyone can tell me if I am doing it the right way in view and then how to access these data in the template?

任何人都可以告诉我,我是否以正确的方式在视图中进行操作,然后如何在模板中访问这些数据?

Thank you!

1 个解决方案

#1


Just cut your view code to this line:

只需将您的视图代码剪切到此行:

entries = Entry.objects.filter(user=request.user).order_by("-timestamp")

And do this in the template:

并在模板中执行此操作:

{% for entry in entries %}
    <td>{{ entry.datadesc }}</td>
    <td><table>
    {% for file in entry.entryfile_set.all %}
        <td>{{ file.datafile.name|split:"/"|last }}</td>
        <td>{{ file.datafile.size|filesizeformat }}</td>
        <td><a href="{{ object.datafile.url }}">download</a></td>
        <td><a href="{% url main.views.delete object.id %}">delete</a></td>
    {% endfor %}
    </table></td>
{% endfor %}

I am a big fan of using related_name in Models, however, so you could change this line:

我很喜欢在Models中使用related_name,所以你可以改变这一行:

entry = models.ForeignKey(Entry)

To this:

entry = models.ForeignKey(Entry, related_name='files')

And then you can access all the files for a particular entry by changing this:

然后,您可以通过更改以下内容来访问特定条目的所有文件:

{% for file in files.entryfile_set.all %}

To the more readable/obvious:

更可读/更明显:

{% for file in entry.files.all %}

#1


Just cut your view code to this line:

只需将您的视图代码剪切到此行:

entries = Entry.objects.filter(user=request.user).order_by("-timestamp")

And do this in the template:

并在模板中执行此操作:

{% for entry in entries %}
    <td>{{ entry.datadesc }}</td>
    <td><table>
    {% for file in entry.entryfile_set.all %}
        <td>{{ file.datafile.name|split:"/"|last }}</td>
        <td>{{ file.datafile.size|filesizeformat }}</td>
        <td><a href="{{ object.datafile.url }}">download</a></td>
        <td><a href="{% url main.views.delete object.id %}">delete</a></td>
    {% endfor %}
    </table></td>
{% endfor %}

I am a big fan of using related_name in Models, however, so you could change this line:

我很喜欢在Models中使用related_name,所以你可以改变这一行:

entry = models.ForeignKey(Entry)

To this:

entry = models.ForeignKey(Entry, related_name='files')

And then you can access all the files for a particular entry by changing this:

然后,您可以通过更改以下内容来访问特定条目的所有文件:

{% for file in files.entryfile_set.all %}

To the more readable/obvious:

更可读/更明显:

{% for file in entry.files.all %}