无法在同一Django视图中使用两个模型

时间:2018-12-30 10:50:58

标签: python django

我对django很陌生,似乎无法在同一视图中创建两个模型。我已经尝试了djano文档中的指南,但似乎无法在html中使用两个不同的模型模板。我是否可能误解了OneToOneField的工作原理?

html仅呈现一个空的div。帐户模板可以正常工作。

Models.py

from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User
from django import template
import phonenumbers

# Create your models here.
class Account(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    site = models.CharField(max_length=50, choices=(('all', 'all'), ('danielk', 'danielk')), blank=True)
    phoneNumber = models.IntegerField()
    birthDate = models.DateField()
    streetAdress = models.CharField(max_length=255)
    zipCode = models.CharField(max_length=4)
    city = models.CharField(max_length=255)

    def formatedPhone(self, country=None):
        return phonenumbers.parse(Account.phoneNumber, "NO")

    def __str__(self):
        return "%s %s" % (self.user.first_name, self.user.last_name)

    def get_absolute_url(self):
        return reverse("account-detail", kwargs={"pk": self.pk})

    class Meta:
        verbose_name = "Account meta"
        verbose_name_plural = "Accounts meta"

class Notes(models.Model):
    userNoted = models.OneToOneField(User, on_delete=models.CASCADE)
    note = models.TextField()
    date = models.DateTimeField((""), auto_now=False, auto_now_add=True)
    active = models.BooleanField(default=True)

    def get_absolute_url(self):
        return reverse("note-detail", kwargs={"pk": self.pk})

    class Meta:
        verbose_name = "Note detial"
        verbose_name_plural = "Notes details"    

Views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.views.generic.detail import DetailView
from django.views.generic.edit import *


# Create your views here.

from .models import *

#Users
class echoUsersOverview(generic.ListView):
    model = Account
    template_name = "echo/users/echo-users-overview.html"

class echoUsersDetail(generic.DetailView):
    model = Account
    template_name = "echo/users/echo-users-detail.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['Account notes'] = Notes.objects.all()
        return context

HTML

<div class="notification is-warning">
                    {{ notes.note }}
                </div>
                <nav class="panel">
                    <p class="panel-heading">
                        User information
                    </p>
                    <label class="panel-block is-paddingless">
                        <table class="table is-fullwidth">
                            <tbody>
                                <tr>
                                    <td style="width: 150px;"><strong>Group</strong></td>
                                    <td>Super Administrator</td>
                                </tr>
                                <tr>
                                    <td><strong>Site manager</strong></td>
                                    <td>
                                    {% if account.site == 'all' %}
                                        <span class="tag is-danger">{{account.site}}</span>
                                    {% elif  account.site == 'danielk' %}
                                        <span class="tag is-info">{{account.site}}</span>
                                    {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>ID</strong></td>
                                    <td>{{account.id}}</td>
                                </tr>
                                <tr>
                                    <td><strong>Birthdate</strong></td>
                                    <td>{{account.birthDate}}</td>
                                </tr>
                                <tr>
                                    <td><strong>Phonenumber</strong></td>
                                    <td><a href="tel:{{account.phoneNumber}}">{{account.phoneNumber}}<a></td>
                                </tr>
                                <tr>
                                    <td><strong>Email</strong></td>
                                    <td><a href="mailto:{{account.user.email}}">{{account.user.email}}</a></td>
                                </tr>
                                <tr>
                                    <td><strong>Streetadress</strong></td>
                                    <td>{{account.streetAdress}}
                                        <br>{{account.zipCode}}
                                    </td>
                                </tr>

                            </tbody>
                        </table>
                    </label>

                    </nav>

1 个答案:

答案 0 :(得分:2)

说实话,如果您只想访问用户的注释,则无需将Notes模型实例传递给上下文。您可以通过以下方式简单地做到这一点:

<div class="notification is-warning">
     {{ object.user.notes }}
</div>

请查看OneToOneField文档。