Django foreign key form field rendering in template - python

I would like to create a view for multiple object deletion. For this, I thought I could use a modelformset_factory.
This are my models:
class Item(models.Model):
rfid_tag = models.CharField()
asset = models.OneToOneField('Assets', default=None, null=True,
on_delete=models.SET_DEFAULT,)
date = models.DateTimeField(name='timestamp',
auto_now_add=True,)
...
class Assets(models.Model):
id = models.AutoField(db_column='Id', primary_key=True)
assettag = models.CharField(db_column='AssetTag', unique=True, max_length=10)
assettype = models.CharField(db_column='AssetType', max_length=150)
...
class Meta:
managed = False
db_table = 'Assets'
ordering = ['assettag']
def __str__(self):
return f"{self.assettag}"
def __unicode__(self):
return f"{self.assettag}"
Below is the form and formset factory:
class ItemDelete(forms.ModelForm):
asset = forms.CharField(required=True,
help_text= "Item asset tag",
max_length=16,
)
delete = forms.BooleanField(required=False,
label="Delete",
help_text='Check this box to delete the corresponding item',
)
class Meta:
model = Item
fields = ['asset']
ItemDeleteMultiple= forms.modelformset_factory(model=Item,
form=ItemDelete,
extra=0,
)
The view:
class DeleteMultipleView(generic.FormView):
template_name = *some html file*
form_class = ItemDeleteMultiple
success_url = *some url*
def form_valid(self, form):
return super().form_valid(form)
And the template:
{% extends "pages/base.html" %}
{% block title %}
<title>Delete Multiple</title>
{% endblock %}
{% block static %}
{% load static %}
{% endblock %}
{% block content %}
<h1>Delete Multiple Items</h1>
<form class="item_delete_multiple_form" action ="." method="POST"> {% csrf_token %}
<table border="2">
<tr><th colspan="3" scope="row">Select Items to Delete</th></tr>
{% for item_form in form %}
<tr>
{% if item_form.non_field_errors %}
<td>{{ item_form.non_field_errors }}</td>
{% endif %}
{% if item_form.asset.errors %}
<td>{{item_form.asset.errors}}</td>
{% endif %}
<td><label for="{{ item_form.asset.id_for_label }}">AssetTag {{forloop.counter}}:</label></td>
<td>{{item_form.asset}}</td>
{% if item_form.delete.errors %}
<td>{{item_form.delete.errors}}</td>
{% endif %}
<td>{{item_form.delete}}</td>
</tr>
{% endfor %}
</table>
</form>
{% endblock %}
{% block script %}
{% endblock %}
The template is not very easy to the eye, so here is the important part: <td>{{item_form.asset}}</td>.
The issue is the following:
If I don't add the asset = CharField() part in the ItemDelete form, the template will render what the __str__ / __unicode__ method of the Assets model will return (the assettag field) in a choice field.
If the asset field is a CharField in the form, the template will render the id of the Assets. The database entry in the Item table.
I would like to render asset.assettag in a CharField (read only text input). Is it possible to do this?
Or is there a better way to achieve the multiple delete operation, using a list of objects and a checkbox?

I have came to the following solution:
class ItemDelete(forms.ModelForm):
asset = forms.CharField(required=True,
help_text= "Item asset tag",
max_length=16,
disabled=True,
)
delete = forms.BooleanField(required=False,
label="Delete",
help_text='Check this box to delete the corresponding item',
)
def __init__(self, *args, **kwargs):
super(ItemDelete,self).__init__(*args, **kwargs)
self.initial['asset'] = Assets.objects.get(id=self.initial['asset']).assettag
class Meta:
model = Item
fields = ['asset']
Given that the input field is used just for presentation purposes (is disabled and cannot be edited), I think it will do. The downside is that it will hit the database for every object (being a formset, every object will have it's own form).
I am still open to better suggestions.

Related

How do I iterate inside a queryset in django template

My django view returns a dictionary people with values for all keys in list format.
The code for the view is:
class ProjectDetail(View):
def get(self, request, pk, *args, **kwargs):
project = Project.objects.get(pk=pk)
roles = Role.objects.filter(project=pk)
people = {}
for role in roles:
try:
people[role] += Person.objects.filter(role=role.pk)
except KeyError:
people[role] = [Person.objects.filter(role=role.pk)]
context = {
'project': project,
'people': people
}
return render(request, 'project_management/project_detail.html', context)
My Models
class Project(models.Model):
title = models.CharField(max_length=2000)
introduction = models.TextField(blank=True)
class Role(models.Model):
role_name = models.CharField(max_length=30)
project = models.ForeignKey(Status, on_delete=models.CASCADE)
class Person(models.Model):
name = models.CharField(max_length=30, blank=True, null=True)
role = models.ForeignKey(Role, on_delete=models.CASCADE)
In order to iterate through the dictionary I used:
{% for designation, persons in people.items %}
<h5> {{ designation.role_name }} </h5>
<ul>
{% for person in persons %} <!-- My Problem lies here, this loop is not iterating, it's running only once-->
<li> {{person}} </li>
{% endfor %}
</ul>
{% endfor %}
The result I got is:
I want the items inside queryset listed out separately, instead of being shown inside square brackets. How can I make that happen?
You don't need to do all this work. You can pass only the project. For example with:
from django.shortcuts import get_object_or_404
class ProjectDetail(View):
def get(self, request, pk, *args, **kwargs):
project = get_object_or_404(Project, pk=pk)
return render(request, 'project_management/project_detail.html', {'project': project})
or even simpler with a DetailView [Django-doc]:
class ProjectDetail(DetailView):
queryset = Status.objects.prefetch_related(
'role_set', 'role_set__person_set'
)
template_name = 'project_management/project_detail.html'
context_object_name = 'project'
Then in the template you can render this with:
{% for role in project.role_set.all %}
<h5> {{ role.role_name }} </h5>
<ul>
{% for person in role.person_set.all %}
<li> {{ person.name }} </li>
{% endfor %}
</ul>
{% endfor %}

How to only display model field value with same data only once in Django templates?

This is my models.py
class report(models.Model):
Citizennumber = models.IntegerField()
Subject = models.CharField(max_length=100)
Patientfile = models.FileField(upload_to="Report")
Description= models.CharField(max_length=200)
Hospitalname= models.CharField(max_length=50, default="blank")
uploaddate = models.DateField(default=datetime.date.today)
How do I loop through this models in django templates so that if 'report.Hospitalname' has same value in multiple objects and I only want to print it once in templates?
First you should order the queryset by Hospitalname.
reports = Report.objects.order_by('Hospitalname')
Then you can use the template tag {% ifchanged %} to print the Hospitalname when it changes through iterations:
{% for report in reports %}
{% ifchanged report.Hospitalname %}
<h1>{{ report.Hospitalname }}</h1>
{% endifchanged %}
...
{% endfor %}

How to query fields from two related modelforms in a Django template?

What I would like to do is show a table listing all authors, and for each author list their respective books. I'm using a form as some of the fields will be modifiable from the page.
models.py
class Author(models.Model):
author_id = models.CharField(max_length=80, primary_key=True)
first_name = models.CharField(max_length=80)
last_name = models.CharField(max_length=80)
class Book((models.Model):
book_id = models.CharField(primary_key=True)
title = models.CharField(max_length=200)A
author_id = models.ForeignKey(Author, on_delete=models.CASCADE)
forms.py
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = '__all__'
So in the view, I'm passing context which contains the two formsets to the template.
views.py
if request.method == 'GET':
author_dbrecords = Author.objects.all()
author_fields = [ f.name for f in Author._meta.get_fields() if not f.is_relation ]
authors_detail_list = [{ field:getattr(author, field) for field in author_fields } for author in author_dbrecords ]
book_dbrecords = Book.objects.all()
book_fields = [ f.name for f in Book._meta.get_fields() if not f.is_relation ]
books_detail_list = [{ field:getattr(book, field) for field in book_fields } for book in book_dbrecords ]
author_formset = AuthorFormSet(initial=authors_detail_list, prefix='author')
book_formset = AuthorFormSet(initial=book_detail_list, prefix='book')
context = {
'author_formset':author_formset,
'book_formset':book_formset,
}
return render(request, 'index.html' context)
In the template I can loop through the individual formsets, but I can't work out how to show all book titles for each author. Just using dot notation as you would for a regular model doesn't work.
index.html
<form action="{% url 'index' %}" method="post">
{{ author_formset.management_form }} {{ book_formset.management_form }}
{% for author in author_formset %}
<tr>
{% for author_field in author %}
<td> {{ author_field }}</td>
{% endfor %}
{{% for book_field in book.author %}
<td> {{ book_field }}</td>
{% endfor %}
I was able to resolve this by effectively accessing the underlying model (rather than form) in the template using ".instance", and using _set for the reverse relationship. I wrote a custom method in order to retrieve all fields from the Book model.
{% for author_field in author %}
<td> {{ author_field }}</td>
{% endfor %}
{% for book in author.instance.book_set.all %}
{% for book_field in book.get_fields %}
<td> {{ book_field }}</td>
{% endfor %}
{% endfor %}
New method in models.py:
def get_fields(self):
return [(field.value_to_string(self)) for field in Book._meta.fields]
Only downside at the moment is that I'm unable to check BookForm's hidden fields in the template.

how to get unique values in django

I have a category model and list of posts related to those category also some post with same category name but when i wanted to make list of category section in template,
it showing duplicate name of category as it related to posts like:
food,
food,
desert,
style,
desert,
but I want like:
food,
desert,
style,
here is my code:
views.py
class ListCategory(ListView):
model = Post
paginate_by = 2
template_name = 'shit.html'
context_object_name = 'queryset'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
cate = Post.objects.all()
context['cate'] = cate
return context
models.py
class Category(models.Model):
title = models.CharField(max_length=20)
thumbnail = models.ImageField()
detail = models.TextField()
featured = models.BooleanField(default=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-category', kwargs={
'pk': self.pk
})
class Post(models.Model):
title = models.CharField(max_length=100)
overview = models.TextField()
featured = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(Author,on_delete=models.CASCADE)
thumbnail = models.ImageField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
tags = TaggableManager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={
'pk': self.pk
})
templates
{% extends "base.html" %}
{% load static %}
{% block content %}
<div class="sidebar-box ftco-animate">
<ul class="categories">
<h3 class="heading mb-4">Categories</h3>
{% for cat in cate %}
<li>{{cat.category}}<span>(12)</span></li>
{% endfor %}
</ul>
</div>
{% endblock content %}
Thank you so much!
Seems like you want to group your Posts, based on their category; so you can achieve that by iterating over the Category (instead of Post), and use the backward relationship to find out the related Post objects.
views.py
class ListCategory(ListView):
model = Category
paginate_by = 2
template_name = 'shit.html' # :)
context_object_name = 'queryset'
template:
{% extends "base.html" %}
{% load static %}
{% block content %}
<div class="sidebar-box ftco-animate">
<ul class="categories">
<h3 class="heading mb-4">Categories</h3>
{% for category in queryset %}
<li>{{category}}<span>{{ category.posts_set.count }}</span></li>
<ul>
{% for post in category.post_set.all %}
<li>{{ post }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
</div>
{% endblock content %}
I also use {{ category.post_set.count }} instead of 12, since I think you are looking for the number of Post objects within each category.
You can use unique=True in desired field, to make every value unique. If you'll try to add new record with same value of unique field, a django.db.IntegrityError will be raised.
More about unique
More about model's fields options

Django- How to make a dynamic template with ManyToManyField in model

I'm new to Django. I'm trying to figure out how to get the tags property of MyModel (manytomany) onto the template (mytemplate.html) for each object in MyModel. Note: there are 4 possible tags in the Tags admin. Some of the objects from MyModel have 2 tags, some have 1. How do I get each object's unique selection of tags onto my template? Here are my files:
models.py
class Tag(models.Model):
CATEGORY_CHOICES = (
('dataset', 'dataset'),
('brand', 'brand'),
)
tag = models.CharField(max_length=100)
category = models.CharField(max_length=100, choices=CATEGORY_CHOICES)
class MyModel(models.Model):
id = models.CharField(max_length=30, primary_key=True)
publish_date = models.DateField()
tags = models.ManyToManyField(Tag)
views.py
from .models import MyModel, Tag
def index(request):
tags = Tag.objects.all()
infos = MyModel.objects.all().order_by('publish_date').reverse()
return render(request, 'mytemplate.html', {'infos': infos, 'tags':tags})
mytemplate.html
Right now this just creates 4 p elements- one for all 4 possible 'tags'
{% for info in infos %}
<small>{{info.publish_date}}</small>
{% for tag in tags %}
<p>{{tag.tag}}</p>
{% endfor %}
{% endfor %}
Try the following
{% for info in infos %}
<small>{{info.publish_date}}</small>
{% for tag in info.tags.all %}
<p>{{tag.tag}}</p>
{% endfor %}
{% endfor %}

Categories