我正在开发一个Django项目,该项目有3个应用程序,即客户,投资和股票。每个客户可以拥有多个投资和股票,但不能相反。到目前为止,一切都在应用程序的管理员端很好地工作。我可以作为管理员CRUD,但我的目标是有3个单独的用户级别 - 客户,顾问和管理员(这工作正常!)。客户只能查看他/她的个人资料,投资和与之相关的股票。顾问可以查看多个客户及其投资组合的信息。我想我可以通过不同的身份验证级别/限制来区分它们。这些是我的文件,
这是我的客户模式,
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Customer(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=200)
cust_number = models.AutoField(max_length=5, primary_key=True)
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zipcode = models.CharField(max_length=10)
email = models.CharField(max_length=200)
home_phone = models.CharField(max_length=50)
cell_phone = models.CharField(max_length=50)
created_date = models.DateTimeField(
default=timezone.now)
updated_date = models.DateTimeField(
blank=True, null=True)
def created(self):
self.created_date = timezone.now()
self.save()
def updated(self):
self.updated_date = timezone.now()
self.save()
def __str__(self):
return self.name
这是我的投资模式,
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Investment(models.Model):
category = models.CharField(max_length=50)
description = models.CharField(max_length=200)
cust_number = models.ForeignKey('customers.Customer')
acquired_value = models.DecimalField(max_digits=10, decimal_places=2)
acquired_date = models.DateTimeField(default=timezone.now)
recent_value = models.DecimalField(max_digits=10, decimal_places=2)
recent_date = models.DateTimeField(default=timezone.now, blank=True, null=True)
def created(self):
self.acquired_date = timezone.now()
self.save()
def updated(self):
self.recent_date = timezone.now()
self.save()
def __str__(self):
return self.category
这是我的股票模型,
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Stock(models.Model):
symbol = models.CharField(max_length=10)
name = models.CharField(max_length=50)
shares = models.CharField(max_length=50)
cust_number = models.ForeignKey('customers.Customer')
purchase_price = models.DecimalField(max_digits=10, decimal_places=2)
recent_date = models.DateTimeField(default=timezone.now, blank=True, null=True)
def created(self):
self.recent_date = timezone.now()
self.save()
def __str__(self):
return self.name
这是我在客户应用中的views.py虽然有投资和股票的单独视图,但我在customers / views.py中定义了相同的类,因此它在单个视图中呈现。
from django.shortcuts import render
from django.utils import timezone
from .models import Customer
from investments.models import Investment
from stocks.models import Stock
def customer(request):
customers = Customer.objects.filter(created_date__lte=timezone.now())
return render(request, 'customers/customer.html', {'customers': customers})
def investment(request):
investments = Investment.objects.filter(created_date__lte=timezone.now())
return render(request, 'customers/customer.html', {'investments': investments})
def stock(request):
stocks = Stock.objects.filter(created_date__lte=timezone.now())
return render(request, 'customers/customer.html', {'stocks': stocks})
我正在尝试在单个html页面上呈现这样的视图,
这是我的customer.html模板
{% load staticfiles %}
<!DOCTYPE html>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<html lang="en">
<head>
<link rel="stylesheet" href="{% static 'css/customers.css' %}">
<meta charset="UTF-8">
<title>Eagle Financial Services</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-primary">
<div class="panel-heading">Welcome!</div>
<div class="panel-body">
ABC Financial Services, your Financial Services Partner.
</div>
</div>
</div>
</div>
</div>
<div class="row">
<h2 style="padding-left: 15Px">Customer Information</h2>
</div>
<div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="bg-info">
<th>Customer ID</th>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Primary Email</th>
<th>Home Phone</th>
<th>Cell Phone</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<tbody>
{% for customer in customers %}
<tr>
<td>{{ customer.cust_number }}</td>
<td>{{ customer.name }}</td>
<td>{{ customer.address }}</td>
<td>{{ customer.city }}</td>
<td>{{ customer.state }}</td>
<td>{{ customer.zipcode }}</td>
<td>{{ customer.email }}</td>
<td>{{ customer.home_phone }}</td>
<td>{{ customer.cell_phone }}</td>
<td><a href="{{ customers.customer }}" class="btn btn-primary">Read</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="row">
<h2 style="padding-left: 15Px">Investments Information</h2>
</div>
<div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="bg-info">
<th>Customer ID</th>
<th>Name</th>
<th>Category</th>
<th>Description</th>
<th>Acquired Value</th>
<th>Acquired Date</th>
<th>Recent Value</th>
<th>Recent Date</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<tbody>
{% for customer in customers %}
<tr>
<td>{{ customer.cust_number }}</td>
<td>{{ customer.name }}</td>
{% for investment in investments %}
<td>{{ investment.category }}</td>
<td>{{ investment.description }}</td>
<td>{{ investment.acquired_value }}</td>
<td>{{ investment.acquired_date }}</td>
<td>{{ investment.recent_value }}</td>
<td>{{ investment.recent_date }}</td>
{% endfor %} {% endfor %}
</tbody>
</table>
</div>
<div class="row">
<h2 style="padding-left: 15Px">Stocks Information</h2>
</div>
<div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="bg-info">
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Cust_Number</th>
<th>Purchase Price</th>
<th>Recent Date</th>
<th colspan="3">Actions</th>
</tr>
</thead>
<tbody>
{% for stock in stocks %}
<tr>
<td>{{ stock.symbol }}</td>
<td>{{ stock.name }}</td>
<td>{{ stock.shares }}</td>
<td>{{ stock.cust_number }}</td>
<td>{{ stock.purchase_price }}</td>
<td>{{ stock.recent_date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
这是projectname.url
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('customers.urls')),
url(r'', include('investments.urls')),
url(r'', include('stocks.urls')),
]
这是customers.url
`from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.customer, name='customer'),
]`
投资和股票类似于customer.urls,除了名称。
现在在网页上,只显示了客户表中的值,而在投资表中,只显示了cust_number和名称,而在库存中没有显示任何内容。我在过去的3天里尝试了几乎所有的试验/错误。我很困惑。我错了。任何指导将受到高度赞赏。感谢。
我正在使用Python - 3.6,Django - 1.11.1。
答案 0 :(得分:1)
在您的模板中,您尝试迭代三个不同的模板变量,通过dict传递到render
,这些变量由三个不同的视图提供服务。为什么在使用特定的其中一个视图时,如果您未将此变量传递给模板以供服务器呈现,然后将其显示给客户端,为什么还要渲染所有这些视图?
您在投资表中获取某些数据(向客户引用数据)的唯一原因是,它从外部(包含)forloop引用客户对象上的数据,迭代您传入的客户对象。< / p>
特别是您正在服务:
def customer(request):
customers = Customer.objects.filter(created_date__lte=timezone.now())
return render(request, 'customers/customer.html', {'customers': customers})
作为主视图'^$'
,其中包含客户对象列表作为模板变量customers
,用于呈现{% for customer in curstomers %}
循环中的数据
替代方法:
除非有什么东西阻止你将模式更改为此...我不明白为什么会这样。您可以只将一个ForeignKey添加到股票和投资中,以表示特定客户的股票和投资,而不是让您的数据尽可能稀疏(尽管......偶尔也是一件好事)。
示例:
class Customer(models.Model):
...
created = models.DateTimeField(default=timezone.now)
**updated = models.DateTimeField(auto_now_add=True)**
stock = models.ForeignKey(Stock, related_name="stocks")
investment = models.ForeignKey(Investment, related_name="investments")
<强>的变化:强>
您可以在实例上将这些引用为customer.stock_set.(filter / all etc)
。同样适用于投资,或者您可以使用相关名称customer.stocks
。此外,如果您愿意,也可以使用prefetch_related
。
注意更新后的auto_now_add=True
您可以将其用于时间戳,而不是手动更新它们,因为它会将时间更改为在模型实例的每次连续保存中保存的时间。
摆脱股票和投资的外键。
其他:
为什么客户没有OneToOneField
引用用户模型实例来定义代表客户的自定义用户模型?
答案 1 :(得分:1)
您遇到此问题的原因是views.customer
仅传递客户信息,而不传递股票等其他变量。
所以在customer/views.py
:
def customer(request):
customers = Customer.objects.filter(created_date__lte=timezone.now())
investments = Investment.objects.filter(created_date__lte=timezone.now())
stocks = Stock.objects.filter(created_date__lte=timezone.now())
return render(request, 'customers/customer.html', {'customers': customers, 'stocks': stock, 'investments', investments})