Here is my Model_Items:
class Model_Items(models.Model):
title = models.CharField(max_length=20, default='')
manufacturer = models.CharField(max_length=10, default='')
make = models.CharField(max_length=20, default='')
model = models.CharField(max_length=20, default='')
sku = models.CharField(max_length=10, default='')
Which the Po has a ManyToMany relationship with:
class Po(models.Model):
po_number = models.CharField(max_length=10, default='')
receive_date = models.DateField(auto_now=False, auto_now_add=False, default='')
model_item = models.ManyToManyField(Model_Items)
site_code = models.CharField(max_length=10, default='')
comment = models.TextField(max_length=50, default='')
Now in my View I query it :
def po_page(request, *args, **kwargs):
po = Po.objects.all()
context = {
'po_list': po,
}
return render(request, 'po/po_page.html', context)
On my Template:
{% for my_po_list in po_list %}
<div class="card">
<table class="table table-bordered" >
<tr>
<td class="col-md-2">{{ my_po_list.po_number }}</td>
<td class="col-md-2">{{ my_po_list.receive_date }}</td>
<td class="col-md-2">{{ my_po_list.model_item.all }}</td>
<td class="col-md-2">{{ my_po_list.site_code }}</td>
<td class="col-md-2">{{ my_po_list.comment }}</td>
<td class="col-md-2">{{ model }}</td>
</tr>
</table>
</div>
{% endfor %}
I have been struggling to get get title from Model_Items class and displaying it.
However the line with:
{{ my_po_list.model_item.all }}
Displays the following instead
<QuerySet [<Model_Items: Mac 16 Dev>, <Model_Items: Mac 13 Std>]>.
I know the line is suppose to do that but how can I filter it so it just displays the Model_Items title.
I would recommend using a for loop to loop through the model items. I also added in what you would do to add commas between the titles.
<td class="col-md-2">
{% for model_item in my_po_list.model_item.all %}
{{model_item. title}}{% if not forloop.last %}, {% endif %}
{% endfor %}
</td>
Related
I'm still a beginner and I'm stuck in a challenging spot for me.
I can't get data from a foreign key table to be inserted "correctly" into a column of a ListView.
I basically want to create a list view of a table (FeatureFilm). This also works. But in one column I get information from another table and here I get data, but not the one that belongs to the particular table row.
Here are my models. The table I want to show is "FeatureFilm" model. This model is inherited from my base Project class "ProjectBaseModel".
Then there is another table "CompanyInvolved Model". This is attached to the FeatureFilm table with a foreign key (feature_id).
So movies are stored (FeatureFilm) and different companies are involved in the creation process of the movies. (CompanyInvolved)
class ProjectBaseModel(models.Model):
title = models.CharField("Titel", max_length=100, blank=False, unique=True)
leading_postproduction_id = models.ForeignKey(
Company,
verbose_name="Federführende Postproduktion",
on_delete=models.SET_NULL,
blank=True,
null=True,
)
phase = models.CharField(choices=post_phase, max_length=30, blank=True, null=True)
former_title = models.CharField("Titel, ehemalig", max_length=100, blank=True)
title_international = models.CharField(
"Titel, international", max_length=100, blank=True, null=True, unique=True
)
class FeatureFilm(ProjectBaseModel):
class Meta:
verbose_name = "Kinofilm"
verbose_name_plural = "Kinofilme"
ordering = ["title"]
class ProductionManager(models.Manager):
def get_production(self):
return (
super()
.get_queryset()
.filter(company_role="Produktion", is_production_list=True)
.values_list("company_involved__name")
)
class CompanyInvolved(models.Model):
feature_id = models.ForeignKey(
FeatureFilm,
on_delete=models.CASCADE,
null=True,
blank=True,
)
tv_movie_id = models.ForeignKey(
TvMovie, on_delete=models.CASCADE, null=True, blank=True
)
company_role = models.CharField(
choices=company_role,
max_length=15,
blank=True,
help_text="Produktion, Co-Produktion, Kinoverleiher, Sender, Weltvertrieb",
)
company_involved = models.ForeignKey(
Company,
on_delete=models.SET_NULL,
null=True,
blank=True,
)
is_production_list = models.BooleanField(
default=False,
verbose_name="Produktion in Liste",
)
productionmanager = ProductionManager()
def __str__(self):
return "#" + str(self.pk)
class Meta:
verbose_name = "Produktion, Co-Produktion, Verleih, Sender, Weltvertrieb"
verbose_name_plural = "Produktion, Co-Produktion, Verleih, Sender, Weltvertrieb"
ordering = ["pk"]
I basically wanted to generate the output now via the template. I can iterate the lines with the template for loop. But I also learned that more complex queries don't work in the DjangoTemplate language, or simply don't belong there.
I don't need every row of data from the CompanyInvolved, but only the company_role = "Production" and is_production_list = True.
A combined "Where" clause in the template now nice, but doesn't exist, so I built myself a MODEL MANAGER (ProductionManager) that does this filtering in the model.
here is the View:
class FeatureListView(LoginRequiredMixin, PermissionRequiredMixin, ListView):
permission_required = "project.can_access_featurefilm_list"
model = FeatureFilm
template_name = "project/feature-list.html"
def handle_no_permission(self):
return redirect("access-denied")
ans here is the depending snippet in the template:
<tbody>
{% for project in object_list %}
<tr>
<td>{{ project.title }}</td>
<td>{{ project.companyinvolved_set.get_production }}
<br>
</td>
<td>{% if project.program_length_planned %}
{{ project.program_length_planned }}
{% endif %}
</td>
<td>{{ project.global_shooting_resolution }}</td>
<td>{{ project.global_resolution_theatrical }}</td>
<td>{% if project.hdr == 1%}
ja
{% else %}
nein
{% endif %}
</td>
<td>{{ project.stafflist.postproduction_supervisor_id.username }}</td>
<td>{% if project.phase %}
{{ project.phase }}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
so I iterate over each movie project with {% for project in object_list %} and then I want to show in my "problem" column the company that has the role production and since there can be more than one, the one that was previously marked by the user for the lists view - > is_production_list = TRue and then the output should come: {{ project.companyinvolved_set.get_production }}.
The result is going in the right direction, but it is still not finally correct. I get the CompanyINvolved data and these are also filtered by company_role = "Production" and is_production_list = True , but these values are now displayed to me each time in each individual Row the same, I get so not per ROW the associated productions but simply each time ALL. I'm missing the reference to the FeatureFilm object, but I don't know how to get this now, or where to put this reference now?
changed my template:
<tbody>
{% for project in object_list %}
<tr>
<td>{{ project.title }}</td>
<td>
!! {{ project.production_companies.company_involved.name}}!!
<br>
</td>
<td>{% if project.program_length_planned %}
{{ project.program_length_planned }}
{% endif %}
</td>
<td>{{ project.global_shooting_resolution }}</td>
<td>{{ project.global_resolution_theatrical }}</td>
<td>{% if project.hdr == 1%}
ja
{% else %}
nein
{% endif %}
</td>
<td>{{ project.stafflist.postproduction_supervisor_id.username }}</td>
<td>{% if project.phase %}
{{ project.phase }}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
I think your code is not working because get_production calls super().get_queryset(), which returns a new QuerySet, without any filters applied, not based on the Queryset in the reverse look up in companyinvolved_set
Your best bet is to add a #property annotation to access the filtered list through your FeatureFilm model
#property
def production_companies(self):
return [company for company in self.companyinvolved_set.all() if company.company_role == 'Produktion' and company.is_production_list]
Then add this to your template:
<td>{% for production in project.production_companies %}{{production.company_involved.name}}{% endfor %}</td>
And to prevent a lot of queries, change the get_queryset method in your view to this:
def get_queryset(self):
queryset = FeatureFilm.objects.prefetch_related('companyinvolved_set').all()
return queryset
app1/model
class Deployment(models.Model):
uid = models.CharField(max_length=255, blank=True)
target = models.CharField(max_length=255)
name = models.CharField(max_length=255, blank=True)
description = models.TextField(blank=True)
app2/model
class AccountRecord(models.Model):
name = models.CharField(max_length=255, blank=True)
file_name = models.CharField(max_length=255, blank=True)
national_id = models.CharField(max_length=255, default="")
app3/views.py
class IndexView(LoginRequiredMixin, ListView):
model = AccountRecord
template_name = "tableview/table_view.html"
paginate_by = 25
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['deployment'] = Deployment.objects.all()
return context
app3 html template
<table class="table card-table table-vcenter text-nowrap">
<thead>
<tr>
<th>Deployment</th>
<th>Account</th>
<th>National ID</th>
<th>Filename</th>
</tr>
</thead>
<tbody>
{% for account in object_list %}
<tr>
<td>DEPLOYMENT.NAME HERE!</td>
<td>{{ account.name }}</td>
<td>{{ account.national_id }}</td>
<td>{{ account.file_name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I am trying to pass data from these 2 model to another apps html template.
with my current app3/views.py the table only show blank column even after i put {{ deployment.name }}
can anyone point out how to do it correctly.
I would like to develop a popup on a list link (please see the screenshot) click. popup will be a list also.
So far this is my code below
Model class:
class PSCInscpection(models.Model):
vessel = models.ForeignKey(Vessel, on_delete=models.CASCADE, null=True)
inspection_id = models.CharField(max_length=100, null=False, unique=True)
followup_inspection = models.BooleanField(null=False, default=False)
date = models.DateTimeField(null=True, blank=True)
port = models.CharField(max_length=50, null=True, blank=True)
country = models.CharField(max_length=50, null=True, blank=True)
number_of_defects = models.PositiveIntegerField(null=True, blank=True)
days_detained = models.PositiveIntegerField(null=True, blank=True)
class PSCInscpectionDefects(models.Model):
pscinscpection = models.ForeignKey(PSCInscpection, on_delete=models.CASCADE, null=True)
inspection_id = models.CharField(max_length=100, null=False, blank=False)
defect_code = models.CharField(max_length=50, null=True, blank=True)
defect_text = models.CharField(max_length=255, null=True, blank=True)
class_is_responsible = models.CharField(max_length=50, null=True, blank=True)
defective_item_code = models.CharField(max_length=50, null=True, blank=True)
views.py
inspections = PSCInscpection.objects.filter(
vessel_id=self.kwargs['pk']).order_by('-date')
renderableInspections = []
for _insp in inspections:
item = {
"date": _insp.date.strftime("%Y-%m-%d"),
"port": _insp.port,
"country": _insp.country,
"defects": _insp.number_of_defects,
"detained": _insp.days_detained
}
renderableInspections.append(item)
context['inspections'] = renderableInspections
My list binding from this "PSCInscpection" object and popup will bind from "PSCInscpectionDefects" object. Any help regarding please ? Thanks !
view.html:
{% for inspection in inspections %}
<tr>
<td>{{ inspection.date }}</td>
<td>{{ inspection.country }}</td>
<td>{{ inspection.port }}</td>
<td>
{% if inspection.defects %}
{{ inspection.defects }}
{% else %}
{{ 0 }}
{% endif %}
</td>
<td>{{ inspection.detained|default:0 }} Days</td>
</tr>
{% endfor %}
You should be able to access the defects tied to each inspection through the reverse foreign key relationship, but currently you can't do that because the context you're sending to the template is not the queryset of objects itself. Instead, you constructed a data structure manually to handle this which isn't necessary.
You should send the context as your object queryset so you can access your reverse foreign key relationship directly in the template with inspections.pscinscpectiondefects_set.all. This is very verbose, so I recommend setting a related_name attribute to the pscinscpection field in your PSCInscpectionDefects model to defects or similar so you can instead access it with inspections.defects.all.
Simplify your view to return the filtered queryset instead.
# views.py
context['inspections'] = PSCInscpection.objects.filter(
vessel_id=self.kwargs['pk']).order_by('-date')
return context
I won't provide HTML/CSS/JS code for the modal because this question is Django related. But you should be able to launch one when the user clicks the link to the number of defects. Here's the template change assuming a related_name is set.
# view.html
{% for inspection in inspections %}
<tr>
<td>{{ inspection.date }}</td>
<td>{{ inspection.country }}</td>
<td>{{ inspection.port }}</td>
<td>
{% if inspection.defects %}
{{ inspection.defects.count }} <!-- link to modal here -->
{% for defect in inspection.defects.all %}
{{ defect }}
{% endfor %}
{% else %}
{{ 0 }}
{% endif %}
</td>
<td>{{ inspection.detained|default:0 }} Days</td>
</tr>
{% endfor %}
I am trying to show names of the cities of a certain countries {{city}}.
For example if I click "USA" it should show the list of the cities in my database.
But I don't know how to filter it right.
I tried this but nothing show up.
Post.objects.filter(country='country').filter(city='city')
This is my model
class Post(models.Model):
title = models.CharField(max_length=255)
country = models.CharField(max_length=255)
city = models.CharField(max_length=255)
address = models.CharField(max_length=255)
email = models.EmailField(max_length=255)
phone = models.CharField(max_length=255)
website = models.CharField(max_length=255)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('users:blog')
This is my view
def cities(request):
context = {
'posts': Post.objects.filter(country='country').filter(city='city')
}
return render(request, 'users/cities.html', context)
This is my template:
{% for post in posts %}
<table class="table table-hover text-left col-sm-6" style="table-layout: fixed; word-wrap: break-word;">
<tbody>
<tr>
<td>{{ post.id }}</td>
<td>{{ post.city }}</td>
</tr>
</tbody>
</table>
{% endfor %}
{% endblock %}
</div>
You can use values_list and call distinct() to get city names from Post model:
views
def cities(request):
queryset = Post.objects.filter(country='country').values_list(
'city', flat=True).distinct().order_by('city')
return render(request, 'users/cities.html', {'cities': queryset})
template
<table>
<tbody>
{% for city in cities %}
<tr>
<td>{{ city }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I playing around with a simple addressbook application and I'd like to show the company of a contact in a DetailView of a contact.
In my template:
<table class="table table-bordered table-condensed" style="width:50%;">
{% for company in companies %}
{% if company.name == contact.company %}
<tr>
<td>{{ company.name }}</td>
<td>{{ company.street }}</td>
<td>{{ company.plz }}</td>
<td>{{ company.city }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
My view:
class ContactView(DetailView):
model = Contact
template_name = 'contact.html'
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
# Companies
context['companies'] = Company.objects.all()
# Return
return context
In my models:
class Company(models.Model):
name = models.CharField(max_length=255,)
and
class Contact(models.Model):
first_name = models.CharField(max_length=255, blank=True, null=True)
last_name = models.CharField(max_length=255,)
company = models.ForeignKey(Company, blank=True, null=True)
What is wrong with the if statement in my template?
Thanks for your help in advance!
You should compare the company itself, not the name.
Change
{% if company.name == contact.company %}
to
{% if company == contact.company %}