How i can compare models in template - django

Why this compare code not work? I want to display all departments where departments filial = filial, but i got failure only. Thanks very much for you answers!
{% for filial in filials %}
{{ filial }}
{% for dep in departments %}
{{ dep }}
{% if dep.Filial == filial.pk %}
IFIFIFIFIF{{ dep.fullname }}
{% endif %}
{% endfor %}
<br>
{% endfor %}
models:
class Filials(models.Model):
Fullname=models.CharField(max_length=30,blank=False)
Entity=models.CharField(max_length=20,blank=True)
City=models.CharField(max_length=15,blank=True)
INN = models.IntegerField(max_length=20,blank=True,null=True)
def __str__(self):
return self.Fullname
def get_absolute_url(self):
return f'/{self.id}'
class Department(models.Model):
Filial=models.ForeignKey(Filials, on_delete=models.CASCADE,related_name='department', blank=True)
Fullname = models.CharField(max_length=30,blank=False)
def __str__(self):
return self.Fullname

Related

Django - how to get all the users in team when using a costumised user model

I am new to django and have a question: I created a CustomUser model within a users app.
I tried
from users.models import CustomUser, Team
team1= Team.objects.first()
users_team1= team1.user.objects.all()
and it doesnt get me the list of users in this Team
class CustomUser(AbstractUser):
bio= models.CharField(max_length=300, null= True, blank=True)
class Team (models.Model):
title = models.CharField(max_length=200)
user= models.ManyToManyField(get_user_model())
date_created= models.DateTimeField(auto_now_add=True, blank=True, null=True)
date_updated= models.DateTimeField(auto_now=True,blank=True, null=True )
def __str__(self):
return self.title
def get_absolute_url(self): # new
return reverse('team_detail', args=[str(self.pk)])
I want created a HTML page
{% extends '_base.html' %}
{% block title %}{{ object.title }}{% endblock title %}
{% block content %}
<div class="team-detail">
<h2>{{ team.title }}</h2>
<p>Team tile : {{ team.title }}</p>
<p>user: {{ team.user }}</p>
</div>
{% endblock content %}
how can i show all the users in a specific Team?
Thanks in advance.
You should do:
from users.models import CustomUser, Team
team1= Team.objects.first()
# lets pass team1 to your template
return render(request, 'template/name.html', {'team': team1})
Your template should be sthg like:
{% extends '_base.html' %}
{% block title %}{{ object.title }}{% endblock title %}
{% block content %}
<div class="team-detail">
<h2>{{ team.title }}</h2>
<p>Team tile : {{ team.title }}</p>
{% for user in team.user.all %}
<p>user: {{ user }}</p>
{% endfor %}
</div>
{% endblock content %}

Reverse Query Through Django Template File

models.py file
class Company(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class Contact(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
def __str__(self):
return self.first_name
views.py file
class company_detail(DetailView):
def get(self, request, *args, **kwargs):
company = get_object_or_404(Company, pk=kwargs['pk'])
context = {'company': company}
return render(request, 'crm/company_detail.html', context)
company_detail.html file
{% extends 'base.html' %}
{% block content %}
<div id="container">
<ul>
<li>{{company.name}}</li>
</ul>
{% for company in object_list %}
{{ company.name }}
{% for Contact in company.contact_set.all %}
{{Contact.first_name}}
{% empty %}
<!-- no entries -->
{% endfor %}
{% endfor %}
</div>
{% endblock content %}
I'm trying to get the Contacts who are under that company to show up on the company_detail.html page. How do I reverse query it properly to show all Contacts under that company?
Thanks in advance
There is no object_list in your DetailView's context, only the object. You need to remove the for loop over object_list in the template
{% for contact in company.contact_set.all %}
{{ contact.first_name }}
{% empty %}
<!-- no entries -->
{% endfor %}

combining two or more querysets from different models in django

Main model:
class MainCategory(models.Model):
title = models.CharField(max_length=120, unique=True)
App#1:
class Category(models.Model):
title = models.CharField(max_length=120, unique=True)
main_category = models.ForeignKey(MainCategory, default=1, related_name='car_category')
App#2:
class Category(models.Model):
title = models.CharField(max_length=120, unique=True)
main_category = models.ForeignKey(MainCategory, default=1, related_name='classifieds_category')
on home page I want a combined list of both category list items as follows.
{% for object in main_cat_list %}
{{ object.title }}
{% for item in object.car_category %}
{{ item.title }}
{% endfor %}
{% endfor %}
How I can insert classifieds category also inside this list?
If you merely want to also display the classified_category as you have the car_category.
{% for object in main_cat_list %}
{{ object.title }}
{% for item in object.car_category %}
{{ item.title }}
{% endfor %}
{% for item in object.classified_category %}
{{ item.title }}
{% endfor %}
{% endfor %}

How to display in template value of foreign key?

models:
class ProductOpinion(models.Model):
user = models.ForeignKey(User)
product = models.ForeignKey(Product)
point = models.IntegerField()
class Product(models.Model):
name = models.CharField(max_length=255)
How to display in my template point of my ProductOpinion?
def index(request):
products = Product.objects.all()
return render_to_response('index.html',{'products':products}, context_instance=RequestContext(request))
template:
{% for p in products %}
{{ p.point }}
{% endfor %}
If yout want to acces the point field you have to do something like this:
{% for p in products %}
{% for productoption in p.productoptions_set.all %}
{{ productoption.point }}
{% endfor %}
{% endfor %}
You need to use:
{{ p.productopinion_set.all }}

ForeignKey. How to get data?

class Property(models.Model):
title = models.CharField(max_length=255)
class CurrentPrice(models.Model):
current = models.ForeignKey(Current)
prop = models.ForeignKey(Property)
price = models.DecimalField(max_digits=5, decimal_places=2)
class Current(models.Model):
name = models.CharField(max_length=20)
views.py:
...
p = Property.objects.all()
return render_to_response('index.html',{'p':p},context_instance=RequestContext(request))
How to get price of Property and display it in my template?
template:
{% for item in p %}
{{ item.title }}
{{ item.price }} # ???
{% endfor %}
I'm not sure what is your purpose/design of models, which doesn't look appropriate from what you have shown.
You will have many CurrentPrice per Property object, so in template you can do is
{% for item in p %}
{{ item.title }}
{% for cp in item.currentprice_set.all %}
{{ cp.price }}
{% endfor %}
{% endfor %}
If Property can have multiple CurrentPrice objects (what is by default):
{% for item in p %}
{{ item.title }}
{% for current_price in item.currentprice_set.all %}
{{ current_price.price }}
{% endofor %}
{% endfor %}
If only one (but in that case it is better to use o2o field instead of the FK fiel else it is up on you to prevent multiple CurrentPrice records pointing to the same Property):
{% for item in p %}
{{ item.title }}
{{ item.currentprice_set.get.price }}
{% endfor %}
I think what you're trying to do is something like that below.
class Property(models.Model):
title = models.CharField(max_length=255)
#property
def current_price(self):
# The current price is the last price that was given.
if self.pricing_history.count() > 0:
return self.pricing_history.order_by('-when')[0].amount
return None
class Price(models.Model):
prop = models.ForeignKey(Property, related_name='pricing_history')
amount = models.DecimalField(max_digits=5, decimal_places=2)
when = models.DateTimeField(auto_now_add=True)
example in template:
{% for item in p %}
{{ item.title }}
{{ item.current_price }}
{% endfor %}