Select reverse Foreign Key in ModelChoicefield - python

I have an order form where the user can choose one item and select a quantity. The price depends on how much is ordered. For example, each item is $10 if you order <100, but $7 if you order 100-200.
In the template, I want to display the pricing information underneath the form for each choice.
These are my Models:
class Product(models.Model):
name = models.TextField()
class Price(models.Model):
"""
This lets us define rules such as:
When ordering <100 items, the price is $10
When ordering 100-200 items, the price is $7
When ordering 200-300 items, the price is $5
etc
"""
price = models.FloatField()
min_quantity = models.PositiveIntegerField()
max_quantity = models.PositiveIntegerField()
product = models.ForeignKey(Product)
class Order(models.Model):
product = models.ForeignKey(Product, null=False, blank=False, default='')
quantity = models.IntegerField()
I can loop over the form fields and the queryset independently:
{% for choice in form.product.field.queryset %}
<h1>{{choice}} {{choice.price_set.all}}</h1>
{% endfor %}
{% for choice in form.product %}
<h1>{{ choice.tag }} {{ choice.choice_label }}</h1>
{% endfor %}
... but I don't know how to combine the loops to display the prices underneath the form fields.
Essentially, I want to select a reverse foreign key from a ModelChoicefield widget. I either need to loop over both the form fields and the queryset at once or access elements in the queryset from the form element. Ideally, this is what I'd like to do in my template:
{% for choice in form.product %}
<h1>{{ choice.tag }} {{ choice.choice_label }}</h1>
{% for price in choice.price_set.all %}
<h1>{{price}} etc...</h1>
{% endfor %}
{% endfor %}
Surely I'm not the first person with this use case. What's the best way to do this?
Edit: As requested, this is my form and my view. Reviewing this, I suppose I should have mentioned I was using the RadioSelect widget.
Form:
class OrderForm(forms.ModelForm):
class Meta:
exclude = ['date_added']
widgets = {
'mailer': forms.RadioSelect
}
model = Order
View:
def processOrder(request):
if request.method == 'POST':
orderForm = OrderForm(request.POST)
if orderForm.is_valid():
orderObject = orderForm.save()
return render(request, TEMPLATE_PREFIX + "confirm.html", {"orderObject": orderObject})
else:
return render(request, TEMPLATE_PREFIX + "register.html", { "form": orderForm })
else:
return render(request, TEMPLATE_PREFIX + "register.html", { "form": OrderForm()})

For (non)perfectionists with deadlines, this code works, albeit inefficiently.
{% for choice in form.product %}
{% for price_candidate in form.mailer.field.queryset %}
{% if price_candidate.id == choice.choice_value|add:0 %}
<h1>{{ choice.tag }} {{ choice.choice_label }}</h1>
{% for price in price_candidate.price_set.all %}
<h1>{{price}} etc...</h1>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
(The add:0 hack converts choice_value into an int. cf http://zurb.com/forrst/posts/Compare_string_and_integer_in_Django_templates-0Az )

Related

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 %}

Django foreign key form field rendering in template

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.

How to show sub-sub category of sub-category in Django?

I have the following models:
class TutorialCategory(models.Model):
category_title = models.CharField(max_length=150)
category_summary = models.CharField(max_length=150)
category_slug = models.SlugField(default=1, blank=True)
class TutorialSeries(models.Model):
series_title = models.CharField(max_length=200)
series_maincategory = models.ForeignKey(
TutorialCategory, default=1, on_delete=models.SET_DEFAULT)
series_summary = models.CharField(max_length=200)
class Tutorial(models.Model):
tutorial_title = models.CharField(max_length=150)
tutorial_content = models.TextField()
tutorial_published = models.DateTimeField(
"date Published", default=datetime.now())
tutorial_series = models.ForeignKey(
TutorialSeries, default=1, on_delete=models.SET_DEFAULT)
tutorial_slug = models.SlugField(default=1, blank=True)
As shown above TutorialCategory is main category, TutorialSeries is sub category and Tutorial is sub-sub-category. I created a simple view that shows sub categories of main categories, but don't know how to show the sub-sub categories of sub category.
Please check out views.py and urls.py if you can improve its quality and if there is an easy and better way of doing it. Anyway, this is view:
def single_slug(request, single_slug):
matching_series = TutorialSeries.objects.filter(
series_maincategory__category_slug=single_slug)
series_urls = {}
for i in matching_series.all():
part_one = Tutorial.objects.filter(
tutorial_series__series_title=i.series_title).earliest("tutorial_published")
series_urls[i] = part_one.tutorial_slug
return render(request, 'tutorial/sub-category.html', context={
"tutorial_series": matching_series,
'part_ones': series_urls
})
urls here:
urlpatterns = [
path('', views.home_page, name='home'),
path('tutorial/<int:id>/', views.tutorial_detail, name='tutorial_detail'),
path('<single_slug>/', views.single_slug, name='single_slug'),
]
the template that shows sub-category of main category:
{% for tut, partone in part_ones.items %}
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ tut.series_title }}</h5>
<p>{{ tut.series_summary }}</p>
Read more
</div>
</div>
{% endfor %}
Please help me how to do it and if you know any better way of doing it please let me know and help me. Thank you so much in advance.
edit: #ruddra
I changed views.py to this passing matching_series
def single_slug(request, single_slug):
matching_series = TutorialSeries.objects.filter(
series_maincategory__category_slug=single_slug)
series_urls = {}
for i in matching_series.all():
part_one = Tutorial.objects.filter(
tutorial_series__series_title=i.series_title).earliest("tutorial_published")
series_urls[i] = part_one.tutorial_slug
return render(request, 'tutorial/sub-category.html', context={
"matching_series": matching_series,
'part_ones': series_urls
})
and replaced the previous template with yours. template here:
{% for tutorial in matching_series %}
{% for sub_cat in tutorial.tutorialseries_set.all %}
{{ sub.series_title }}
{% for sub_sub_cat in sub.tutorial_set.all %}
{{ sub_sub_cat.tutorial_title }}
{% endfor %}
{% endfor %}
{% endfor %}
You can try like this:
{% for sub_cat in matching_series %}
{% for sub_sub_cat in sub_cat.tutorial_set.all %}
{{ sub_sub_cat.tutorial_title }}
{% endfor %}
{% endfor %}
Here I am assuming matching_series is being passed through context from view in single_slug. Then I am using backward relation between different models to fetch the data.
Explanation: Assuming there is object named tutorial_category which is instance of TutorialCategory. As there is ForeignKey from TutorialSeries to TutorialCategory, I can use tutorial_category.tutorialseries_set.all() or .filter() etc to fetch the tutorial series from tutorial_category object( As I am rendering it in template, I removed parenthesis after all). Similarly I fetch Tutorial from TutorialSeries.

How can I render values from a loop from views to template?

I'm creating a private 1 to 1 chat between users and I'd like to show the last sent message, the problem is that when I'm looping every room's last message to my template it only shows me one single data and there are many.
Here is what I have in my views.py:
def my_rooms(request, username):
user = User.objects.get(username=username)
room_user = Room.objects.all()
print('----------------------------------------')
for room in room_user:
rooms = room.messages.all()[:1]
print(rooms)
return render(request, "my_room.html", {
'rooms': rooms,
})
print(rooms), (each article has their own private chat room):
-------------------------------
[]
[<Message: 02-02-2017 09:50AM, user01 to user02 : hey>] #room1
[]
[<Message: 02-02-2017 10:52AM, user01 to user02 : Fine and you ?>] #room2
my_room.html
{% for room in rooms %}
<p>{{ room.client }} : {{ room.message }}</p>
<p>{{ room.timestamp }}</p>
{% endfor %}
What happens in my template ?
I just get :
[<Message: 02-02-2017 10:52AM, user01 to user02 : Fine and you ?>] #room2
's datas published but NOT
[<Message: 02-02-2017 09:50AM, user01 to user02 : hey>] #room1
Why so ? How can I show all concerned rooms datas ?
------ UPDATE ------
The problem I'm trying to solve is that I'd like return values from the for loop to my template. I just don't understand why it's showing only 1 result and not all the for looped results on my template?
models.py might help it a bit clearer :
class Room(models.Model):
gig = models.ForeignKey(Gig, null=True)
creator = models.ForeignKey(User)
...
class Message(models.Model):
client = models.ForeignKey(User, related_name='client', null=True)
seller = models.ForeignKey(User, related_name='seller', null=True)
room = models.ForeignKey(Room, related_name='messages', null=True)
message = models.TextField(max_length=500)
timestamp = models.DateTimeField(default=timezone.now, db_index=True)
...
Thanks to Daniel Roseman's comment, who suggested me to append results to a list I could resolve this issue.
Here is how I did it :
views.py
...
room_user = Room.objects.all()
my_rooms = []
for room in room_user:
rooms = room.messages.all()[:1]
my_rooms.append(rooms)
return render(request, "my_room.html", {
'rooms': my_rooms,
})
my_room.html
{% for room in rooms %}
{% for r in room %}
<p>{{ r.client }} {{ r.message }}</p>
<p>{{ r.timestamp }}</p>
{% endfor %}
{% endfor %}
Looking at the flow of your views.py code, it's only going to pass the rooms object on the last iteration of the for loop into your template. I would suggest passing room_user instead to the dictionary:
return render(request, "my_room.html", {
'room_user': room_user,
}
Then, in your template, you'll want to build a nested for loop:
{% for room in room_user %}
{% for rmessage in room.messages.all()[:1] %}
<p>{{ rmessage.client }} : {{ rmessage.message }}</p>
<p>{{ rmessage.timestamp }}</p>
{% endfor %}
{% endfor %}

How to display multiple ForeignKey filtered items in a single view in django?

I'm trying to understand the best way to display ForeignKey filtered data in a Django model.
I have three models reduced to this:
// models.py
class Publisher(models.Model)
def publisher_name = models.TextField()
def publisher_slug = models.SlugField()
def founded_year = models.IntegerField()
class Album(models.Model)
def album_name = models.TextField()
def publisher = models.ForeignKey('Publisher', related_name='albums')
class Song(models.Model)
def song_name = models.TextField()
def album = models.ForeignKey('Album', related_name='songs')
def published_year = models.IntegerField()
I have a URL that is composed of: /<publisher>/<published_year>/
The view I'm having trouble composing is supposed to be details like this:
Title of: Publisher.publisher_name
List of All Albums by the publisher: List of All songs from that album published the same year as the publisher__published_year: List of All songs from that album published as the url
The way, I've tried to do this that works right now is similar to this:
// views.py
class SongYearView(TemplateView):
def get_context_data(self, **kwargs):
context = super(SongYearView, self).get_context_data(**kwargs)
context['publisher'] = Publisher.objects.get(slug=kwargs['publisher_slug']
album_list=[]
for album in context['publisher'].albums.all():
single_album = dict()
single_album['album'] = album
single_album['publisher_year_song'] = album.songs.filter(published_year=context['publisher'].published_year)
single_album['filtered_year_song'] = album.songs.filter(published_year=kwargs['published_year']
album_list.append(single_album)
context['albums'] = album_list
return context
Then in the template I'm doing (with stripped out formatting)
// template.html
{{ publisher.name }}
{% for album in albums %}
{{ album.album.album_name }}
{% for song in album.publisher_year_song %}
{{ song.song_name }}
{% endfor %}
{% for song in album.filtered_year_song %}
{{ song.song_name }}
{% endfor %}
{% endfor %}
While this does work, it's not pretty and I'm pretty sure there are better ways of doing this.
This is an odd example, but just a basic example of my more detailed models. The way to think about it is Publisher -> Album -> Song or A -> B -> C. And I'm trying to get a view of all B items, that are only linked with a specific A item and then get two sets of C items for each B item, where one set is filtered on an A property and the other set is filtered on a passed argument from a URL.
I tried to get a custom model.Manager to help get this constructed, but didn't have much luck.
You could do add a custom template filter of_year:
#register.filter
def of_year(songs, year):
return songs.filter(published_year=year)
And change your template to
// template.html
{{ publisher.name }}
{% for album in publisher.albums %}
{{ album.album.album_name }}
{% for song in album.songs|of_year:publisher.founded_year %}
{{ song.song_name }}
{% endfor %}
{% for song in album.songs|of_year:filtered_year %}
{{ song.song_name }}
{% endfor %}
{% endfor %}
And clean your view:
// views.py
class SongYearView(TemplateView):
def get_context_data(self, **kwargs):
context = super(SongYearView, self).get_context_data(**kwargs)
context['publisher'] = Publisher.objects.get(slug=kwargs['publisher_slug'])
context['filtered_year'] = kwargs['published_year']
return context
Edit: rename the template filter

Categories