1👍
The reason why you are facing this problem is that views.customer
only passes the information of customer and not the other variables like stocks.
So in your 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})
1👍
In your template your attempting to iterate three different templates variables, passed in through the dict to render
, which are being served by three different views. Why would you expect all of them to render when using a specific one of these views when you haven’t passed this variable to the template for rendering by the server before it’s displayed to the client?
The only reason you get some data in the investments table (referencing data to customers) is because it’s referencing the data on a customer object from the outer (containing) forloop iterating the customer objects you passed in.
Specifically you’re serving:
def customer(request):
customers = Customer.objects.filter(created_date__lte=timezone.now())
return render(request, 'customers/customer.html', {'customers': customers})
as the home view '^$'
which has a list of customer objects as the template variable customers
which is rendering the data in your {% for customer in curstomers %}
loops
Alternative Approach:
Unless there’s something preventing you from changing the schema to this… which I don’t see why there would be. You can just add a ForeignKey to stocks and investments to denote that specific customers stocks and investments rather than having your data as sparse as it is (although.. that’s occasionally a good thing).
Example:
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")
Changes:
You can reference these as customer.stock_set.(filter / all etc)
on an instance. Similarly for investment, or you can use customer.stocks
using the related name. Additionally you can also use prefetch_related
if you wanted.
Note the auto_now_add=True
on updated you can use this for timestamps rather than manually updating them since it will change the time to be the time it was saved on each successive save of the model instance.
Get rid of the foreign keys on stocks and investments.
Additional:
Why not have a OneToOneField
on your customer referencing a User model instance to define a custom user model representing customers?
- [Answered ]-Can not add bootstrap class with button using Django and Python
- [Answered ]-Django Admin: variables/constants inside Django Admin that can be changed
- [Answered ]-How i can upload an image with CreateView on my post?