gpt4 book ai didi

python - 无法在同一个 django View 中使用两个模型

转载 作者:行者123 更新时间:2023-12-01 01:19:31 25 4
gpt4 key购买 nike

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

html 只是渲染一个空的 div。帐户模板工作正常。

模型.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"

View .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>

最佳答案

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

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

请检查OneToOneField与此相关的文档。

关于python - 无法在同一个 django View 中使用两个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53977020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com