I have two lists of objects, buildnumbers and partrequestnumbers.
Both list contain buildnumbers, and what I am trying to achieve is a html list of the buildnumbers with their associated parturequestnumbers, the link being the buildnumber. So far I have had a look at numpy arrays and a few for loop suggestions without achieving what I am trying to achieve. My current for loop just loops through all of the numbers...
The image shows the build list, I'd like the partnumbers next to it too
Django Views:
def manufacturelist(request, mug=None, slug=None):
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
partrequests = PartRequestNumbers.objects.order_by('-id')
latest_preparebuild_list = PrepareBuild.objects.order_by('-pub_date')
builds=[]
for preparebuild in latest_preparebuild_list:
bb = preparebuild.buildno
builds.append(PartRequestNumbers.objects.filter(buildno=bb))
context = {
"latest_preparebuild_list": latest_preparebuild_list,
"partrequests": partrequests,
"builds": builds,
}
return render(request, "buildpage/manufacturelist.html", context)
HTML:
{% if latest_preparebuild_list %}
<ul>
{% for preparebuild in latest_preparebuild_list %}
<li><a class = "subtitle" >{{ preparebuild.buildno }}: </a><a style= "float: right" href="/buildpage/{{ preparebuild.part_request }}/manufacturebuild">{{ partrequests.part_request }}</a><p style= "float: right">{{ preparebuild.build_status }}</p></li>
{% for partrequest in partrequests %}
<center>{{ partrequest.part_request }} </center>
{% endfor %}
{% endfor %}
</ul>
{% else %}
<p>No builds are available.</p>
{% endif %}
models:
class PrepareBuild(models.Model):
pub_date = models.DateTimeField(default= datetime.now)
STATUS = Choices('Complete', 'In-Build', 'Awaiting Build', 'Cancelled', 'Design Issue', 'M/C Issue')
buildstatus = StatusField()
status_changed = MonitorField(monitor='buildstatus')
buildno = models.CharField(_('Build Number'),max_length=10, default= bnumber)
current_location = models.ForeignKey(BuildLocation, default='', null=True)
machine = models.ForeignKey(Machine, max_length=10, default ="")
batchno = models.CharField(_('Batch Number'),max_length=100, default="")
partnumber = models.CharField(_('Part Number(s)'), max_length = 100, default = 'e.g. 002109_1')
buildtime = models.CharField(_('Build Time'), default= '', max_length=100)
buildnotes = models.CharField(_('Build Notes'), max_length=200, default = "", blank=True)
mug = models.SlugField(unique=True, null=True)
def __unicode__(self):
return self.buildno or default
class PartRequestNumbers(models.Model):
buildno = models.CharField(_('Build Number'),max_length=10, default= bnumber)
part_request = models.ForeignKey(PartRequest, related_name='PR Number+', null=True)
def __str__(self):
return '{}' .format(self.part_request)
Related
I'm trying to loop over my FeatureCatergories, FeatureSubcategories and Features. I'm able to loop over my feature categories just fine. Now I want to loop over my feature subcategories and finally features. I'm not sure what to call in my template for subcategories.. Should it be {{featuresubcategory.title}}? What about features?
views.py
def features_view(request):
context = {
"feature_categories": FeatureCategory.objects.prefetch_related('featuresubcategory_set').all(),
}
return render(request=request, template_name="main/features.html", context=context)
template.html
{% for category in feature_categories %}
{{category.title}}
{% for subcategory in featuresubcategory %}
{{ subcategory.title }}
{% endfor %}
{% endfor %}
models.py
class FeatureCategory(models.Model):
title = models.CharField(max_length=50)
featured_image = models.ImageField(blank=True, upload_to="features/")
category_slug = AutoSlugField(null=True, default=None,
unique=True, populate_from='title')
class Meta:
verbose_name_plural = "Feature Categories"
def __str__(self):
return self.title
class FeatureSubcategory(models.Model):
title = models.CharField(max_length=50)
category = models.ForeignKey('FeatureCategory', on_delete=models.CASCADE)
category_slug = AutoSlugField(null=True, default=None,
unique=True, populate_from='title')
class Meta:
verbose_name_plural = "Feature Subcategories"
def __str__(self):
return self.title
class Feature(models.Model):
title = models.CharField(max_length=150)
category = models.ManyToManyField(FeatureSubcategory)
description = models.TextField()
featured_image = models.ImageField(upload_to=image_dir)
class Meta:
verbose_name_plural = "Features"
def __str__(self):
return self.title
In your template, you must change
featuresubcategory with category.featuresubcategory_set.all()
also,
use prefetch_related('featuresubcategory') instead of prefetch_related('featuresubcategory_set')
{% for category in feature_categories %}
{{category.title}}
{% for subcategory in category.featuresubcategory_set.all %}
{{ subcategory.title }}
{% endfor %}
{% endfor %}
For features of subcategory, add related_name to your category field
category = models.ManyToManyField(FeatureSubcategory,related_name='subcategory_features')
and now, you can use it in your template, and don't forget to make migrate
{% for category in feature_categories %}
{{category.title}}
{% for subcategory in category.featuresubcategory_set.all %}
{{ subcategory.title }}
{% for feature in subcategory.subcategory_features.all %}
{{feature.title}}
{% endfor %}
{% endfor %}
{% endfor %}
You can see related objects reference here
I have a list that is generated by a method on one of my models. On the home page it works wonderfully, however when I go to a detail view of one project I can access all the parts of that project as they are direct fields of the Model, but I can't access the items in the list.
Model:
class Project(models.Model):
date_published = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
area = models.ForeignKey(Area, on_delete=models.PROTECT)
title = models.CharField(max_length=128, unique=True)
slug = models.SlugField(max_length=64)
summary = models.CharField(max_length=256)
others = models.CharField(max_length=128, blank=True)
deadline = models.DateField(null=True, blank=True)
priority = models.ForeignKey(Priority, on_delete=models.PROTECT)
closed = models.DateTimeField(null=True, blank=True)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.title)
super(Project, self).save(*args, **kwargs)
#property
def updates(self):
updates = []
sequence_id = 1
categories = set(self.update_set.all().values_list(
'category__id', flat=True))
for cat_id in categories:
a = Update.objects.filter(
project=self, category__id=cat_id).order_by('added').last()
if cat_id == sequence_id:
updates.append(a)
else:
for i in range(cat_id - sequence_id):
updates.append('null')
updates.append(a)
sequence_id = cat_id
sequence_id += 1
return updates
class Update(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
category = models.ForeignKey(UpdateCategory, on_delete=models.PROTECT)
update = models.TextField(max_length=240, blank=True)
added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.update
The view is simple:
class ProjectDetailView(DetailView):
template_name = 'project_portal/project_detail.html'
queryset = Project.objects.all()
and here is the dynamic url that I am using:
path('project/<int:pk>/',
ProjectDetailView.as_view(), name='project_detail'),
As for the template, I'm lost, here is one of the things I have tried:
<!DOCTYPE html>
{% extends "project_portal/base.html" %}
{% block home %}
<div id="main">
<div id="content">
<div>
<h1>{{ object.title }}</h1>
<h1>hello</h1>
{% if object_list %}
{% for item in updates %}
<p>{{ item }}</p>
{% endfor %}
{% else %}
<h2>No records found for this project</h2>
{% endif %}
</div>
</div>
</div>
{% endblock %}
What do I need to do to access the "updates" list that gets generated?
update is a property of the model instance, you need to access it from there like any other attribute. Also note, there is no object_list in a detail view.
<div>
<h1>{{ object.title }}</h1>
<h1>hello</h1>
{% for item in object.updates %}
<p>{{ item }}</p>
{% endfor %}
</div>
Template:
{% for level in levels %}
{% if level.todo_set.all in tasks %}
<li>
<img src="{{ level.badge.url }}" alt="" />
</li>
{% else %}
<li>
<img src="{{ level.locked_badge.url }}" alt="" />
</li>
{% endif %}
{% endfor %}
views.py:
#login_required(login_url="/account/login/")
def StudentPublicProfile(request, pk=None):
student = User.objects.get(pk=pk)
levels = Level.objects.all()
todos = ToDo.objects.all()
list = []
tasks = Task.objects.filter(student=student)
for task in tasks:
list.append(task.todo)
context = {"student": student, "levels": levels, "tasks": list, "todos": todos}
return render(request, "student_public_profile2.html", context)
models.py:
class Level(models.Model):
number = models.IntegerField()
badge = models.ImageField()
locked_badge = models.ImageField()
timestamp = models.DateTimeField(
auto_now_add=True, auto_now=False, blank=True, null=True
)
unlock = models.CharField(max_length=10, default="A")
def __str__(self):
return str(self.number)
def get_absolute_url(self):
return reverse("student:level-detail", kwargs={"pk": self.pk})
class ToDo(models.Model):
level = models.ForeignKey(Level, on_delete=models.CASCADE)
name = models.CharField(max_length=150)
description = models.TextField()
timestamp = models.DateTimeField(
auto_now_add=True, auto_now=False, blank=True, null=True
)
def __str__(self):
return self.name
class Task(models.Model):
level = models.ForeignKey(Level, on_delete=models.CASCADE)
todo = models.ForeignKey(ToDo, on_delete=models.CASCADE)
student = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=150)
content = models.TextField()
timestamp = models.TimeField(auto_now=True)
datestamp = models.DateField(auto_now=True)
like = models.ManyToManyField(User, related_name="user_likes", blank=True)
is_verified = models.BooleanField(default=False, blank=True)
def __str__(self):
return self.title
Basically, each level contains multiple todos. I want to check if all todos of the level are contained in the tasks list. This is what I'm doing in the template. But I'm not getting a correct result. What could be the issue here? Here I'm checking if all todos of a level are completed by a particular user. Only when a todo is completed it gets saved in Task
You are probably best to handle this inside your view.
for level in levels_list:
if Todo.objects.filter(level=level).count() == Task.objects.filter(student=student, level=level).count():
return (level completion code)
levellist = []
for level in levels:
if (
ToDo.objects.filter(level=level).count()
== Task.objects.filter(student=student, level=level).count()
):
levellist.append(level.number)
perc = (len(levellist) / len(levels)) * 100
In Template:
{% for level in levels %}
{% if level.number in levellist %}
<li>
<a href="javascript:;"><img src="{{ level.badge.url }}"
alt=""/></a>
</li>
{% else %}
<li>
<a href="javascript:;"><img src="{{ level.locked_badge.url }}"
alt=""/></a>
</li>
{% endif %}
{% endfor %}
Found a fix!
I'm making a comment system for my django app and i've been told it's best to make a seperate model for comment-voting. So i've done that and here's my models.py:
class Comment(models.Model):
user = models.ForeignKey(User, default=1)
destination = models.CharField(default='1', max_length=12, blank=True)
author = models.CharField(max_length=120, blank=True)
comment_id = models.IntegerField(default=1)
parent_id = models.IntegerField(default=0)
comment_text = models.TextField(max_length=350, blank=True)
timestamp = models.DateTimeField(default=timezone.now, blank=True)
def __str__(self):
return self.comment_text
class CommentScore(models.Model):
user = models.ForeignKey(User, default=1)
comment = models.ForeignKey(Comment, related_name='score')
upvotes = models.IntegerField(default=0)
downvotes = models.IntegerField(default=0)
def __str__(self):
return str(self.comment)
Here's my views.py where the comments are created:
def article(request, category, id):
name = resolve(request.path).kwargs['category']
for a, b in CATEGORY_CHOICES:
if b == name:
name = a
instance = get_object_or_404(Post, id=id, category=name)
allauth_login = LoginForm(request.POST or None)
allauth_signup = SignupForm(request.POST or None)
#comments
comment = CommentForm(request.POST or None)
ajax_comment = request.POST.get('text')
comment_length = len(str(ajax_comment))
comment_list = Comment.objects.filter(destination=id)
score = CommentScore.objects.filter(comment=comment_list)
if request.is_ajax():
if comment.is_valid():
comment = Comment.objects.create(comment_text=ajax_comment, author=str(request.user), destination=id)
print(comment)
comment.save()
score = CommentScore.objects.create(comment=comment)
score.save()
username = str(request.user)
return JsonResponse({'text': ajax_comment, 'text_length': comment_length, 'username': username})
else:
print(comment.errors)
context = {
'score': score,
'comment_list': comment_list,
'comment': comment,
'instance': instance,
'allauth_login': allauth_login,
'allauth_signup': allauth_signup
}
return render(request, 'article.html', context)
So the comment works fine, but as you can see a couple lines later i'm trying to then create a CommentScore instance to match with the comment. In my template, I've rendered each comment and it's fields (comment_text, author etc), but I want to render the upvotes field associated with that comment. How would I do this?
template
{% for i in comment_list %}
<div class='comment_div'>
<h3>{{ i.author }}</h3>
<p>{{ i.comment_text }}</p><br>
</div>
{% endfor %}
forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = [
'comment_text',
'id',
'author',
'destination',
]
I've already tried the following and they haven't worked;
{% for i in comment_list %}
<div class='comment_div'>
<h3>{{ i.author }}</h3>
<p>{{ i.comment_text }}</p><br>
{% for i in comment_list.score_set.all %}
{{ i.upvotes }} #renders nothing
{% endfor %}
</div>
{% endfor %}
{% for i in comment_list %}
<div class='comment_div'>
<h3>{{ i.author }}</h3>
<p>{{ i.comment_text }}</p><br>
{% for j in i.score %}
{{ j.upvotes }} #Error: 'RelatedManager' object is not iterable
{% endfor %}
</div>
{% endfor %}
Having a lot of trouble so help is appreciated.
Changing "i.score" to "i.score.all" resolves the problem as the RelatedManaager error usually happens when you are trying to iterate over the manager and not the objects selected by that manager. - Solved by #joe-j
So it works now but if someone could explain the 2nd line of this syntax that would be great:
comment_list = Comment.objects.filter(destination=id)
score = CommentScore.objects.filter(comment=comment_list)
What exactly is happening when I assign comment=comment_list here? I copied this code from someone else but i'm still abit unsure how it's working.
I seem to have a problem with calling a method from a django template. Given the model:
class Gallery(models.Model):
class Meta:
verbose_name_plural = "Galerie"
def photo_count(self):
return self.photo_set.count()
def random_image(self):
return self.photo_set.get(id=random.randint(1,self.photo_count()))
title = models.CharField(max_length=50)
imgur_id = models.CharField(max_length=15)
date_created = models.DateTimeField('Data utworzenia', auto_now=True)
is_active = models.BooleanField()
def __unicode__(self):
return self.title
where Gallery is foreignKey for Photo
views.py:
def index(request):
galleries = Gallery.objects.get(is_active=True)
if galleries.count() is 0:
messages.warning(request, "Niestety, żadna galeria nie jest aktywna, zaglądnij niebawem!")
return redirect(reverse(home))
elif galleries.count() is 1:
return render(request, 'gallery/home.html', {'gallery': galleries})
else:
return render(request, 'gallery/index.html', {'galleries': galleries})
I want to do this in template:
{% for gallery in galleries %}
{{ gallery.random_image }} <br />
{% endfor %}
The result I get is:
[ Photo object ]
[]
[]
[]
But when i call
{% for gallery in galleries %}
{{ gallery.photo_count }} <br />
{% endfor %}
The result is correct.
Why does this not work?
In my opinion you are complicated a little bit.
class Gallery(models.Model):
class Meta:
verbose_name_plural = "Galerie"
def random_image(self):
return self.photo_set.order_by('?')[0] if self.photo_set.count() else None
title = models.CharField(max_length=50)
imgur_id = models.CharField(max_length=15)
date_created = models.DateTimeField('Data utworzenia', auto_now=True)
is_active = models.BooleanField()
def __unicode__(self):
return self.title
and in the template,
{% for gallery in galleries %}
{% if gallery.random_image %}
{{ gallery.random_image }} <br />
{% else %}
No images found.
{% endif %}
{% endfor %}
Read more on order_by here
Note that ? could prove slightly heavy. If so, get thelist of ids of the photo_set associated with this gallery object, and get a random id from this list, rather than doing a
random.randint(1,self.photo_count())
Add #property to your fonction.
#property
def photo_count(self):
return self.photo_set.count()