我正在使用基于类的视图 (TemplateView) 来显示来自用户和用户配置文件模型(用户和用户配置文件之间的一个)的一些信息,并且这按预期工作。 我决定使用相同的 TemplateView 来显示来自 2 个模型的一些其他数据,但我不明白为什么我无法检索和显示信息。 我怀疑这是因为其中一个模型(UserSubscription)有 2 个 fks,一个是 User 模型,另一个是“Subscription”模型。 我尝试在 fks 中使用 related_name 但不确定我是否正确使用它。 代码如下:
models.py
class UserSubscription(models.Model):
user = models.ForeignKey(User, related_name= 'tousers', related_query_name='touser', on_delete=models.CASCADE, null=True, blank=True)
subscription = models.ForeignKey("Subscription", related_name = 'tosubscriptions', related_query_name='tosubscription',on_delete=models.CASCADE, null=True, blank=True)
created_date = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name="Created Date")
subs_status = models.BooleanField(default=False, verbose_name="Subscription Status")
expiry_date = models.DateTimeField(auto_now=False, auto_now_add=False, verbose_name="Expiry Date")
class Meta:
verbose_name = "User Subscription"
verbose_name_plural = "User Subscriptions"
def __str__(self):
return 'User: ' + str(self.user) + ' ' + 'PK: ' + str(self.pk) + ' Subscription :' + str(self.subscription) + ' ' + 'Expiring the : ' + str(self.expiry_date)
class Subscription(models.Model):
plan_name = models.CharField(max_length=50, null=True, blank=True, verbose_name="Subscription Plan Name")
description = models.TextField(verbose_name="Subscription Plan Description")
price = models.FloatField(verbose_name="Subscription Plan Price")
start_date = models.DateTimeField(auto_now=False, auto_now_add=False, verbose_name="Subscription Plan Start Date")
end_date = models.DateTimeField(auto_now=False, auto_now_add=False, verbose_name="Subscription Plan End Date")
active = models.BooleanField(default=False)
class Meta:
verbose_name = "Subscription"
verbose_name_plural = "Subscriptions"
def __str__(self):
return self.plan_name
views.py
from django.shortcuts import render, redirect
from django.views.generic import TemplateView, UpdateView, ListView
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from .models import *
from .forms import *
class MyProfileView(TemplateView):
template_name = 'myprofile.html'
def get_context_data(self, **kwargs):
context = super(MyProfileView, self).get_context_data(**kwargs)
context['User'] = User.objects.all()
context['UserProfile'] = UserProfile.objects.all()
context['subscription'] = Subscription.objects.all()
context['usersubscription'] = UserSubscription.objects.all()
return context
myprofile.html
{% extends 'base.html' %}
{% block title %}
My Profile
{% endblock %}
{% block content %}
<p>Username: {{user.username}}</p>
<p>First Name: {{user.first_name}}</p>
<p>Last Name: {{user.last_name}}</p>
<p>Birth Date: {{user.userprofile.birth_date}}</p>
<p>Email: {{user.email}}</p>
<p>Language: {{user.userprofile.language}}</p>
<p>Address: {{user.userprofile.address}}</p>
<p>Postal Code: {{user.userprofile.postal_code}}</p>
<p>Country: {{user.userprofile.country}}</p>
<form action ='{% url "updatemyprofile" %}'>
<button type="submit">Update My Profile</button>
</form>
<hr>
<p>My Current Plan:{{usersubscription.subscription.plan_name}}</p>
<p>Start Date: {{usersubscription.created_date}}</p>
<p>End Date: {{usersubscription.expiry_date}}</p>
<p>Renew:</p>
<hr>
{% endblock %}
urls.py
from django.urls import path
from . import views as gecviews
urlpatterns = [
path('myprofile/', gecviews.MyProfileView.as_view(), name='myprofile'),
path('updatemyprofile/', gecviews.profileUpdate, name='updatemyprofile'),
结果:
用户名:foo
名字:foo
姓氏:bar
出生日期:2020 年 2 月 2 日
邮箱:foo@bar.com
语言:中文
地址:foobar大道
邮政编码:1000
国家:美国
我目前的计划:
开始日期:
结束日期:
续订:
答案 0 :(得分:0)
这是因为您在上下文['usersubscription'] 中拥有的是一个查询集。
在您看来试试这个:
data: URL
或者您可以在模板中更改这些:
URL.createObjectURL
编辑
context['usersubscription'] = UserSubscription.objects.get(user=request.user)
答案 1 :(得分:0)
我通过执行 .last() 来解决这个问题,如下所示:
UserSubscription.objects.filter(user=self.request.user).last()
结果:
我目前的计划:1 年
开始日期:2021 年 3 月 9 日,晚上 7:49
结束日期:2021 年 3 月 9 日晚上 7:49
续订我的订阅
我的账单:
账单 1:50.0 欧元