I have models which is Player and Position. Player is associated with position player is currently at. You can assign a player new position, which is why I need it to use it in some form. But in that form I see that Position is position Object (see image bellow), but I would like to put "Position.name" in there to see the name of choices instead that object, but I have no Idea how to do it.
Thank you for your help.
models.py
class Position(models.Model):
name = models.CharField(max_length=20)
short = models.CharField(max_length=2)
class Players(models.Model):
name = models.CharField(max_length=200)
positionId = models.ForeignKey(Position, on_delete=models.CASCADE, null=True, blank=True)
forms.py
class CompositionForm(ModelForm):
class Meta:
model = Players
fields = ("positionId", ... many more ... )
table.html
<table id="nominated-player-table" class="table table-bordered" style="">
{% for form in formset.forms %}
{% if forloop.first %}
<thead>
<td colspan="15"
style="background-color: dimgray; color: white; border-top-right-radius: 15px; border-top-left-radius: 15px; border: none">
Nominovaní hráči
</td>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr>
{% for field in form.visible_fields %}
<td>
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
You need add to your models the magic methods __str__ or __unicode__ , because Django itself don't know how to convert model instances to string, and represents it as ModelName object
# In python 2.x you can uncoment next line.
# from __future__ import unicode_literals
class Position(models.Model):
name = models.CharField(max_length=20)
short = models.CharField(max_length=2)
def __unicode__(self):
return self.name
class Players(models.Model):
name = models.CharField(max_length=200)
positionId = models.ForeignKey(
Position, on_delete=models.CASCADE, null=True, blank=True)
def __unicode__(self):
return self.name
If you need more pesonalization, you can use label_from_instance in the form:
class CustomModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return 'The position with name %s' % obj.name
class CompositionForm(forms.ModelForm):
positionId = CustomModelChoiceField(queryset=Position.objects)
class Meta:
model = Players
fields = ("positionId", ... many more ... )
I ran into this the other day, but with a ModelChoiceField in an ordinary form, not a ModelForm. You subclass ModelChoiceField to override the choices labels generation. From the Django docs:
from django.forms import ModelChoiceField
class MyModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return "My Object #%i" % obj.id
In your case include obj.PositionId.name in the label
Something similar should be possible for ModelForms. Check docs. Yes, modelform_factory has a field_classes argument so you can override the default ModelChoiceField
Related
I am trying to show number of articles in each category in my django project. But it shows category id instead of category_name. I want to display category_name and the corresponding number of articles.
blog/views.py
def searchView(request):
statistics = Post.objects.values('cid').annotate(num_articles = Count('cid')).order_by()
return render(request, 'blog/search.html', {'statistics': statistics})
blog/search.html -> here stat.cid shows the category id but I want to show category_name here.
{% extends 'users/base.html' %}
{% block content %}
<div class="container">
<br>
<div class="row text-center">
<div class="col-md-3"> </div>
<div class="col-md-6">
<h4 class="p-2 mb-2 bg-secondary text-white">POPULAR CATEGORIES!!</h4>
<table id="myTable" class="table table-bordered table-hover table-striped shadow-sm bg-white rounded">
<thead>
<tr>
<th>Category</th>
<th>Articles Available</th>
</tr>
</thead>
<tbody>
{% for stat in statistics %}
<tr>
<td>
{{ stat.cid }}
</td>
<td>
{{ stat.num_articles }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endblock content %}
blog/models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth import get_user_model
from django.urls import reverse
from ckeditor.fields import RichTextField
# Create your models here.
class Category(models.Model):
cid = models.AutoField(primary_key=True)
category_name = models.CharField(max_length=100)
def __str__(self):
return self.category_name
class Post(models.Model):
aid = models.AutoField(primary_key=True)
image = models.ImageField(default='blog-default.png', upload_to='images/')
title = models.CharField(max_length=200)
content = RichTextField()
created = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
cid = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='specialization')
approved = models.BooleanField('Approved', default=False)
like = models.ManyToManyField(get_user_model(), related_name='likes', blank=True)
def __str__(self):
return self.title
Post.objects.values('cid') would only give you the pk of the Category. To access the category_name of the Category you should also include that in the values():
# Note the double underscore between cid and category_name
`Post.objects.values('cid', 'cid__category_name')`
Now that the category_name is available, access it in the template like this:
{{ stat.cid__category_name }}
While this is specific to your case, a better answer is here:
https://stackoverflow.com/a/27181936/10951070
I would be going at this from the opposite direction, meaning I would be accessing this data from Category rather than from Post which for some reason you call statistics.
First off I'd suggest you to use a ListView and then I'd proceed as follows:
# views
from django.views.generic import ListView
class CategoryListView(ListView):
model = Category
template_name = 'blog/search.html'
context_object_name = "categories"
# template
<table>
<thead>
...
</thead>
<tbody>
{% for category in categories %}
<tr>
<td>{{ category.cid }}</td>
<td>{{ category.post_set.count }}</td>
</tr>
{% endfor %}
</tbody>
</table>
If you add a related_name in your Post model in the cid attribute and call it posts, you can then in the template write category.posts.count which is more readable. This is just a personal preference and definitely not a rule.
I am looking for the best way to make an updateable list from model objects in Django.
Let's say that I have a models.py:
class Foo(models.Model):
bar = models.CharField(max_length=10, blank=True)
foo = models.CharField(max_length=30, blank=True)
and I have a views.py that shows the objects from that model:
def my_view(request):
person_list = Person.objects.all()
ctx= {'person_list': person_list}
return render(request, 'my_app/my_view.html', ctx)
Then I have a template, my_view.html:
...
<table>
<tr>
<th>bar</th>
<th>foo</th>
<th></th>
</tr>
{% for item in person_list %}
<tr>
<td>{{item.bar}}</td>
<td>{{item.foo}}</td>
<td style="width: 5%;"><button type="submit" name="button">Change</button></td>
</tr>
{% endfor %}
</table>
...
So, I would like to add a form and make one of those fields changeable from within this template.
I would like users to be able to change item.foo and then click the change button and it sends the update to the model.
I tried making it a form, and using forms.py to create a form where users can put an input, and then submit the form, and that looked like this, my_view.html:
...
...
<table>
<tr>
<th>bar</th>
<th>foo</th>
<th></th>
</tr>
{% for item in person_list %}
<form method="post">
{% csrf_token %}
<tr>
<td>{{item.bar}}</td>
<td>{{item.foo}}</td>
<td>{{form.foo}}</td>
<td style="width: 5%;"><button type="submit" name="button">Change</button></td>
</tr>
</form>
{% endfor %}
</table>
...
...
And that was not working, because I couldn't figure out where to send the PK for that particular item in the Model.
Any help is appreciated.
You can do it using class-based views very easily
Say this is your models.py
class MyModel(models.Model):
alpha = models.CharField(max_length=200)
beta = models.CharField(max_length=200)
gama = models.CharField(max_length=200)
delta = models.CharField(max_length=200)
Your views.py should be like this
from django.views.generic import UpdateView
from .models import MyModel
class MyModelUpdateView(UpdateView):
model = MyModel
fields = ['beta'] # Include the fields you want to update form your template
As a reference, this will be your CreateView in views.py file
class MyModelCreateView(CreateView):
model = MyModel
template_name = 'myapp/myapp_form.html'
fields = ['alpha', 'beta', 'gama', 'delta']
This will be your urls.py
from .views import MyModelUpdateView
urlpatterns = [
....
path('app/<int:pk>/update/', MyModelUpdateView.as_view(), name='model-update'),
....
]
The default template for UpdateView is the one used for CreateView, myapp_form.html in this case. This will be your basic form template
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ form }}
<button type="submit">Create</button>
</form>
{% endblock %}
I have an app for example:
class example(models.Model, TemplateHelperClass):
reviewer = CharField(max_length=255, verbose_name="Title")
book = ForeignKey(books, on_delete=models.PROTECT)
review = TextField()
...
class books(models.Model, TemplateHelperClass):
author = CharField(max_length=255, verbose_name="Author")
book_title = CharField(max_length=255, verbose_name="Title")
publisher = CharField(max_length=255, verbose_name="Author")
def __str__(self):
return self.book_title
...
class templateHelperClass():
paginate_by = 15
def get_fields(self):
return [(field, field.value_to_string(self)) for field in self._meta.fields]
*views.py*
class bookReviewList(ListView):
model = example
template_name = "bookreviews.html"
...
*bookreviews.html*
{% extends "base.html" %}
{% block content %}
<table class="table">
<thead>
<tr>
{% for fld, val in object_list.0.get_fields %}
<th>{{ fld.verbose_name |title }}</th>
{% endfor %}
<th></th>
</tr>
</thead>
<tbody>
{% for object in object_list %}
<tr>
{% for fl1, val1 in object.get_fields %}
<td>{{val1}}</td>
{% endfor %}
<td><a class="btn btn-dark" href='{{ object.get_absolute_url }}'>Update</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
The output html displays the pk for the book, and not the book title (the __str__ method) in this case. I'd like to display the __str__ method. Two questions, both related:
(1) Is there a more elegant way to represent the form with my attributes in table format. (I'm using bootstrap for some of the formatting)
(2) If not, how do I get the foreignkey field to display the __str__ return and not the pk?
The TemplateHelperClass is a MixIn for multiple models and the equivalent template for bookreview.html is likewise used for multiple views so I don't want to have to use specific field names in either if I can avoid it on the principle of DRY.
The easy solution is to use custom logic instead of field.value_to_string(...) method
class templateHelperClass():
paginate_by = 15
def get_fields(self):
return [(field, str(getattr(self, field.name))) for field in self._meta.fields]
OR,
use the custom logic only for FK field, as
class templateHelperClass:
paginate_by = 15
def get_fields(self):
return [
(field, str(getattr(self, field.name)))
if isinstance(field, models.ForeignKey)
else (field, field.value_to_string(self))
for field in self._meta.fields
]
I am relatively new to Django but the main problem I am facing right now is to create a ListView that will display uploaded documents based on the primary key of my ForeignKey.
I have tried several methods of trying to create the filter and read the online documentation on class-based view but it does not seem to have relevant information on how to use the primary key of my ForeignKey in my filter.
These are my models:
class Post(models.Model):
title = models.CharField(max_length=100)
image = models.ImageField(default = 'default0.jpg',
upload_to='course_image/')
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=6)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
rating = models.IntegerField(default = 0)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk' : self.pk})
class Lesson(models.Model):
title = models.CharField(max_length=100)
file = models.FileField(upload_to="lesson/pdf")
date_posted = models.DateTimeField(default=timezone.now)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('lesson_upload', kwargs={'pk': self.pk})
Here is my ListView with the filter that is not working:
class LessonListView(ListView):
model = Lesson
template_name = 'store/uploaded_lesson.html'
context_object_name = 'lesson'
# def get_queryset(self):
# return Lesson.objects.filter(Post__pk=self.Post.pk)
def get_queryset(self):
self.post__pk = get_object_or_404(post__pk,
name=self.kwargs['post__pk'])
return Lesson.objects.filter(post__pk=self.post__pk)
Here is my urls.py:
path('post/<int:pk>/lesson_uploaded/', LessonListView.as_view(), name='lesson_uploaded'),
Here is my html:
{% extends "store/base.html" %}
{% block content %}
<div id="main">
<table class="table mb-0">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Download</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for lesson in lesson %}
<tr>
<td>
{% if lesson.file %}
<img src="{{ lesson.file.url }}" style="width:100px;">
{% else %}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
You can try like this:
In urls, add post_id :
path('lessons/<int:post_id>/', LessonListView.as_view()),
Then update the View to get the post_id in get_queryset method:
class LessonListView(ListView):
model = Lesson
template_name = 'store/uploaded_lesson.html'
context_object_name = 'lesson'
def get_queryset(self):
return Lesson.objects.filter(post_id=self.kwargs.get('post_id'))
Also, please don't name list and item of that list in a for loop same, so update it to:
{% for l in lesson %}. // or anything other than lesson
<tr>
<td>
{% if l.file %}
I'm trying to expand the Django tutorial for a school project and make it into a more usable voting app.
What I want is to allow users to create Polls and invite other registered users by email to vote on their Poll. Only the invited users will be allowed to vote.
My models.py:
class Poll(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published', auto_now_add=True)
is_active = models.BooleanField(default=True)
activation_date = models.DateTimeField('Activation Date', blank=True, null=True)
expiration_date = models.DateTimeField('Expiration Date', blank=True, null=True)
public_key = models.CharField(max_length=30, blank=True)
hash = models.CharField(max_length=128, blank=True)
timestamp = models.DateTimeField(auto_now=True)
def __str__(self):
return self.question_text
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
class Choice(models.Model):
question = models.ForeignKey(Poll, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
class EligibleVoters(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
email = models.EmailField(null=True)
I have a Poll table which contains the Poll Title and other information regarding the Poll. I also created a separate Choices table (like in the tutorial) which has a ForeignKey to the Poll table and contains the poll choices.
I figured that in order to invite users as eligible voters I needed a third table with ForeignKeys to the Poll and User tables. So i created one.
I should note that I'm using the build-in user model.
Here's my views.py:
class NewPoll(CreateView):
model = Poll
fields = ['question_text', 'is_active', 'activation_date', 'expiration_date']
success_url = reverse_lazy('voting:index')
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
if self.request.POST:
data['choices'] = ChoiceFormSet(self.request.POST)
# data['eligible_voters_poll'] = EligibleVotersFormSetPoll(self.request.POST)
# data['eligible_voters_user'] = EligibleVotersFormSetUser(self.request.POST)
#data['eligible_voters'] = EligibleVotersFormSet(self.request.POST)
else:
data['choices'] = ChoiceFormSet()
# data['eligible_voters_poll'] = EligibleVotersFormSetPoll()
# data['eligible_voters_user'] = EligibleVotersFormSetUser()
# data['eligible_voters'] = EligibleVotersFormSet()
return data
def form_valid(self, form):
context = self.get_context_data()
choices = context['choices']
# eligible_voters_poll = context['eligible_voters_poll']
# eligible_voters_user = context['eligible_voters_user']
#eligible_voters = context['eligible_voters']
with transaction.atomic():
self.object = form.save()
if choices.is_valid():
choices.instance = self.object
choices.save()
# if eligible_voters_poll.is_valid() and eligible_voters_user.is_valid():
# eligible_voters_poll.instance = self.object
# eligible_voters_poll.save()
# eligible_voters_user.instance = self.object
# eligible_voters_user.save()
#if eligible_voters.is_valid():
# eligible_voters.instance = self.object
# eligible_voters.save()
return super().form_valid(form)
I have commented the lines that are previous attempt into making it work. Without the commented lines the user is able to create a poll and also create choices. I'm having trouble making the invite part working though.
Here is my forms.py:
class PollForm(ModelForm):
activation_date = forms.DateTimeField(required=False)
expiration_date = forms.DateTimeField(required=False)
class Meta:
model = Poll
fields = ['question_text', 'is_active', 'activation_date', 'expiration_date']
class ChoiceForm(ModelForm):
class Meta:
model = Choice
exclude = ['votes']
ChoiceFormSet = inlineformset_factory(Poll, Choice, form=ChoiceForm, extra=1)
def form_maker(parent2):
class EligibleVotersForm(ModelForm):
# def __init__(self, user, poll, *args, **kwargs):
# self.user = user
# self.poll = poll
# super().__init__(*args, **kwargs)
def save(self, commit=True):
instance = super(EligibleVotersForm, self).save(commit=False)
# instance.parent1 = parent1
instance.parent2 = parent2
if commit:
instance.save()
return instance
class Meta:
model = EligibleVoters
fields = ['email']
return EligibleVotersForm
# EligibleVotersFormSetPoll = inlineformset_factory(Poll, EligibleVoters, form=EligibleVotersForm, extra=1)
# EligibleVotersFormSetUser = inlineformset_factory(User, EligibleVoters, form=EligibleVotersForm, extra=1)
# EligibleVotersFormSet = inlineformset_factory(Poll, EligibleVoters, form=form_maker(User), extra=1)
Here I have commented out again the lines that belong to my failed attempts at making it work. The closer I got it to working is to be able to invite by any email (not just the registered ones, which is what I want) and fill the EligibleVoters table with the emails associated with the poll_id. The user_id column though remained, empty.
Here's my html file:
{% extends 'base.html' %}
{% block content %}
{% load static %}
<h2>Poll Creation</h2>
<div class="col-md-4">
<form action='' method="post">
{% csrf_token %}
{{ form.as_p }}
<table class="table">
{{ choices.management_form }}
{% for form in choices.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %} formset_row1">
{% for field in form.visible_fields %}
<td>
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<table class="table">
{{ eligible_voters.management_form }}
{% for form in eligible_voters.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %} formset_row2">
{% for field in form.visible_fields %}
<td>
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<input type="submit" value="Save"/> back to the list
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="{% static 'voting/js/jquery.formset.js' %}"></script>
<script type="text/javascript">
$('.formset_row1').formset({
addText: 'add poll choice',
deleteText: 'remove',
prefix: 'choice_set'
});
$('.formset_row2').formset({
addText: 'add eligible voter',
deleteText: 'remove',
prefix: 'eligiblevoters_set'
});
</script>
{% endblock %}
Any ideas on how to make it work and only allow he eligible voters to vote for the polls they are invited to?
I was thinking that maybe my EligibleVoters model is wrong somehow and I need a ManyToMany field somewhere??