Incorrect Context Appearing in Django Project - python

I am creating a workout project where for every workout there is a list of exercises and for every exercise there is a list of sets which has specific weights and reps.
Here is the model for more clarification:
class Workout(models.Model):
name = models.CharField(max_length = 30,blank=True, null=True)
date = models.DateField(blank=True, null=True)
def __str__(self):
return str(self.date) + ' ' + self.name
class Exercise(models.Model):
training = models.ForeignKey(Workout, on_delete=models.CASCADE, related_name='exercises',blank=True, null=True)
name = models.CharField(max_length = 30, blank=True, null=True)
def __str__(self):
return self.name
class Set(models.Model):
exercise = models.ForeignKey(Exercise, on_delete=models.CASCADE, related_name='sets',blank=True, null=True)
weight = models.FloatField(validators=[MinValueValidator(0)],blank=True, null=True)
repetitions = models.IntegerField(validators=[MinValueValidator(1)],blank=True, null=True)
order = models.IntegerField(validators=[MinValueValidator(1)],blank=True, null=True)
def __str__(self):
return self.exercise.name + ' set #' + str(self.order)
I am trying to show the list of rep in each set for a specific excercise in the template page but I keep getting errors such as:
activity() missing 1 required positional argument: 'request'
or even nothing is showing at all. The most recent view I coded shows all the sets for all the excercises which is not the objective.
Here is the views:
def activity(self,request, **kwargs):
template_name = 'my_gym/start_workout.html'
excercises = Exercise.objects.all().order_by('id')
sets = Set.objects.filter(
set=self.object).order_by('id')
context = {
'excercises': excercises,
'sets': sets,
}
return render(request, template_name, context)
I also tried:
def activity(request):
template_name = 'my_gym/start_workout.html'
excercises = Exercise.objects.all().order_by('id')
sets = Set.objects.all().order_by('id')
context = {
'excercises': excercises,
'sets': sets,
}
return render(request, template_name, context)
Here is also another trial using filter:
def activity(request):
template_name = 'my_gym/start_workout.html'
excercises = Exercise.objects.all().order_by('id')
sets = Set.objects.filter(sets=set.excercises).order_by('id')
context = {
'excercises': excercises,
'sets': sets,
}
return render(request, template_name, context)
I got this error:
type object 'set' has no attribute 'excercises'
Here is the template:
{% for set in sets %}
<td>{{set.order}}</td>
<td>{{set.repetitions}}</td>
{% endfor %}
My question is how to show the sets only for the selected excercise.
A more info there is a previous page which shows all th excercises and when one specific exercise is selected I am expecting to see the reps related to this specific excercise only not the reps for all the exercises in the database.
Thank you

As I did in my project(it was blog):
In urls.py I created new page with category_id(=Category primary key) like this:
path('category/<int:category_id>/', get_category, name='category')
Now in get_category function I get additional parameter that meant the pk of the category to which it belongs. And in views.py:
def get_category(request, category_id):
records = Recordings.objects.filter(category=category_id)
category = Category.objects.get(pk=category_id)
context = {
'records': records,
'category': category,
'title': category.title,
}
return render(request=request, template_name='blog/category.html', context=context)
I just filtered them by category.pk and it worked fine
In your case Recording = Set, Category = Exercise, category_id = exercise_id
Or if you use another way of creating pages for exercised - pls comment, it will be interesting to find a solution for such a model
P.S. This is early version of my project, there should be used select_related for request optimizing
P.P.S Optimized version of view + urls
# urls.py
path('category/<int:category_id>/', RecordingsByCategory.as_view(), name='category')
# views.py
class RecordingsByCategory(ListView):
model = Recordings
template_name = 'blog/category.html'
context_object_name = 'records'
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = Category.objects.get(pk=self.kwargs['category_id'])
return context
def get_queryset(self):
# kwargs goes from urls.py
return Recordings.objects.select_related('category').filter(category=self.kwargs['category_id'], is_published=True)

Related

Django - NOT NULL constraint failed

I'm currently working on a Django app that will parse the contents of an uploaded log file to the associated database in my Django project. I've managed to get it all running as expected except it won't associate my uploaded data with the model's ForeignKey. I can assign null=True which resolves the integrity error but then of course, it doesn't assign any of the uploaded data to that ForeignKey. Here's the code:
models.py
class Case(models.Model):
case_ref = models.CharField(max_length=8)
oic = models.CharField(max_length=50)
subject = models.CharField(max_length=100)
submitted_date = models.DateTimeField(default=datetime.now, blank=True)
def get_absolute_url(self):
return reverse('case_list', kwargs={'pk': self.pk})
def __str__(self):
return self.case_ref + " " + self.subject
class TeamviewerLogs(models.Model):
case = models.ForeignKey(Case, on_delete=models.DO_NOTHING)
teamviewer_id = models.IntegerField()
teamviewer_name = models.TextField()
connection_start = models.TextField()
connection_end = models.TextField()
local_user = models.TextField()
connection_type = models.TextField()
unique_id = models.TextField()
def get_absolute_url(self):
return reverse('case_list', kwargs={'pk': self.pk})
def __str__(self):
return str(self.teamviewer_id) + " - " + str(self.teamviewer_id)
forms.py
class UploadLog(forms.ModelForm):
file = forms.FileField()
class Meta:
model = TeamviewerLogs
fields = [
'file'
]
views.py
def add_logs(request, pk):
case = get_object_or_404(Case, pk=pk)
if request.method == 'POST':
form = UploadLog(request.POST, request.FILES)
if form.is_valid():
teamviewer = form.save(commit=False)
teamviewer.case = case
log_file = request.FILES['file']
log_file = filter(None, (line.rstrip() for line in log_file))
for lines in log_file:
split = lines.decode('utf-8').split('\t')
teamviewer_id = split[0]
teamviewer_name = split[1]
connection_start = split[2]
connection_end = split[3]
local_user = split[4]
connection_type = split[5]
unique_id = split[6]
teamviewer = TeamviewerLogs(teamviewer_id=teamviewer_id, teamviewer_name=teamviewer_name,
connection_start=connection_start, connection_end=connection_end,
local_user=local_user, connection_type=connection_type, unique_id=unique_id)
teamviewer.save()
return redirect('tv_log_details', pk=case.pk)
form.save()
else:
form = UploadLog()
return render(request, 'teamviewer/add_logs.html', {'form': form})
But when I click to upload the file I'm hit with:
When it tries to execute teamviewer.save().
I've been trying to resolve this issue for hours and have tried so many different variations of answers from Stackoverflow or previous code I've used that has worked for different models but I've hit a brick wall...hard!
Any help anyone can offer would be greatly appreciated.
Ok, so here's an example of the concept I've suggested in the comments.
I've got a view which passes some data to the a form;
class ListingDetailView(DetailView):
""" Listing detail page """
model = Listing
template_name = 'listing.html'
def get_form_kwargs(self):
"""Return the kwargs for the form"""
kwargs = {}
initial = {
'listing': self.object,
}
kwargs['initial'] = initial
return kwargs
def get_form(self):
form = ApplicationSignupForm(
**self.get_form_kwargs()
)
return form
def get_context_data(self, **kwargs):
""" Add our form to the context """
context = super().get_context_data(**kwargs)
context['form'] = self.get_form()
return context
The form then makes use of that initial data and sets the field it relates to as hidden. I don't validate this data, but I'll try to show how you might do that;
class ApplicationSignupForm(forms.ModelForm):
class Meta:
""" Setup the form """
fields = (
'listing',
...
)
model = Application
widgets = {
'listing': forms.HiddenInput()
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
initial_data = kwargs['initial']
self.listing = initial_data.get('listing')
def clean(self):
"""
Custom form cleaning
"""
cleaned_data = super().clean()
listing = cleaned_data.get('listing')
if listing != self.listing:
self.add_error('listing', "You can't modify this value")
return cleaned_data

how to work with Multiple forms in django Detailview

I have a comment section in django blog and there are two forms one is for comment another is for reply to the comment but the comment form is working fine and reply form doesn't work! i was trying to do but getting error... IntegrityError at /page/9/
FOREIGN KEY constraint failed...
appreciate to your help :)
Thank you.
views.py
class PostDetailView(DetailView):
model = Post
template_name = "post_detail.html"
context_object_name = 'post'
form = CommentForm()
def get_object(self):
obj = super().get_object()
if self.request.user.is_authenticated:
PostView.objects.get_or_create(
user=self.request.user,
post=obj
)
return obj
def get_context_data(self, **kwargs):
category_count = get_category_count()
most_recent = Post.objects.order_by('-timestamp')[:3]
context = super().get_context_data(**kwargs)
context['most_recent'] = most_recent
context['page_request_var'] = "page"
context['category_count'] = category_count
context['form'] = self.form
return context
def post(self, request, *args, **kwargs):
form = CommentForm(request.POST)
form = ReplyForm(request.POST)# how to work with this form like above from
if form.is_valid():
post = self.get_object()
form.instance.user = request.user
form.instance.post = post
form.save()
return redirect(reverse("post-detail", kwargs={
'pk': post.pk
}))
models.py
class Reply(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now_add=True)
content = models.TextField()
comment = models.ForeignKey('Comment', related_name='replies',default=False, null=True,
on_delete=models.CASCADE)
def __str__(self):
return self.content
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
timestamp = models.DateTimeField(auto_now_add=True)
content = models.TextField()
post = models.ForeignKey('Post', related_name='comments', default=False,
on_delete=models.CASCADE)
def __str__(self):
return self.content
You might find it easier if you did not struggle with trying to persuade a Class-based view to do what it was not intended to do, and instead used a plain old Function-based view.
Here is a two-form view. The code has been refactored into what I regard as a better pattern, to validate both forms and redisplay if anything is wrong at the top, and then you just do the actual work to create and save the objects at the bottom.
def receive_uncoated( request): #Function based view
# let's put form instantiation in one place not two, and reverse the usual test. This
# makes for a much nicer layout with actions not sandwiched by "boilerplate"
# note any([ ]) forces invocation of both .is_valid() methods
# so errors in second form get shown even in presence of errors in first
args = [request.POST, ] if request.method == "POST" else []
batchform = CreateUncWaferBatchForm( *args )
po_form = CreateUncWaferPOForm( *args, prefix='po')
if request.method != "POST" or any(
[ not batchform.is_valid(), not po_form.is_valid() ]):
return render(request, 'wafers/receive_uncoated.html', # can get this out of the way at the top
{'batchform': batchform,
'po_form': po_form,
})
#POST, everything is valid, do the work
# create and save some objects based on the validated forms ...
return redirect( 'wafers:ok' )

How do i query model managers in my function based views for single_page(detail.html)

I'm finding it difficult to query a custom django model manager in my function based detail view. How can i resolve this?
I'm using django 2.2.
in my models.py file, i have this code below, which works perfectly. Querying for the listview is working fine and when i query the detail view using get_object_or_404(Modelname, id), the detail views works too but when i try to query it using my custom model manager, i keep getting this error
"movie_detail() got an unexpected keyword argument 'id'".
i've also tried removing--->
def get_absolute_url(self):
return reverse('core:movie_detail', args=[self.id,])
from my movie model when querying the model manager, but am still get same error
How can i resolve this?
my models.py
class MovieManager(models.Manager):
def all_with_related_persons(self):
qs = self.get_queryset()
qs = qs.select_related('director')
qs = qs.prefetch_related('writers', 'actors')
return qs
class Movie(models.Model):
NOT_RATED = 0
RATED_G = 1
RATED_PG = 2
RATED_R = 3
RATINGS = (
(NOT_RATED, 'NR - Not Rated'),
(RATED_G, 'G - General Audiences'),
(RATED_PG, ' PG - Parental Guidance' 'Suggested'),
(RATED_R, 'R - Restricted'),
)
title = models.CharField(max_length=140)
plot = models.TextField()
year = models.PositiveIntegerField()
rating = models.IntegerField(choices=RATINGS, default=NOT_RATED)
runtime = models.PositiveIntegerField()
website = models.URLField(blank=True)
director = models.ForeignKey(to='Person',
on_delete=models.SET_NULL,
related_name="directed",
null=True,
blank=True)
writer = models.ManyToManyField(to="Person", related_name="writing_credits", blank=True)
actors = models.ManyToManyField(to="Person", through="Role", related_name="acting_credits", blank=True)
objects = MovieManager()
def __str__(self):
return '{} ({})'.format(self.title, self.year)
def get_absolute_url(self):
return reverse('core:movie_detail', args=[self.id,])
class Meta:
ordering = ('-year', 'title',)
<--- Views.py --->
My List view
def movie_list(request):
object_list = Movie.objects.all()
paginator = Paginator(object_list, 12)
page_number = request.GET.get('page', 1)
try:
page = paginator.page(page_number)
except PageNotAnInteger:
# If page is not an integer deliver the first page
page = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
page = paginator.page(paginator.num_pages)
context = {
'object_list': object_list,
'products': page,
'page': page,
}
template = 'core/movie_list.html'
return render(request, template, context)
DetailView without custom model manager.(This works)
def movie_detail(request, id):
object_list = get_object_or_404(Person, id=id)
context = {'movie': object_list}
template = 'core/person_detail.html'
return render(request, template, context)
DetailView with custom model manager.(This doesn't work. throws an error "movie_detail() got an unexpected keyword argument 'id'")
def movie_detail(request):
object_list = Movie.objects.all_with_related_persons()
context = {'movie': object_list}
template = 'core/movie_detail.html'
return render(request, template, context)
My url path to the detail_view
path('<id>/', views.movie_detail, name="movie_detail"),
I expect detail view to return queries based on what i queried in my custom model manager.
Your url pattern for the movie_detail view is passing an id kwarg to your view, so your view needs to accept this id as argument. Instead of
def movie_detail(request)
you should define
def movie_detail(request, id)
The error you see just says: movie_detail was called with keyword argument id, meaning it was called like this: movie_detail(request=the_request, id=some_id) which can't work if it's defined to only have one argument, request.
But also, since you're making a detail view for one object, you should probably do something with the id to select the specific object:
def movie_detail(request, id):
object_list = Movie.objects.all_with_related_persons()
try:
movie = object_list.get(id=id)
except Movie.DoesNotExist:
raise Http404
context = {'movie': movie}
template = 'core/movie_detail.html'
return render(request, template, context)

how to get() this model that belongs to userprofile.team

I'm struggling getting this view to work. In the code I have included a comment that indicated where the issue is. Basically I can not for the life of me get the TeamsWeeklyMasterSchedule object that relates to the EmployeeProfile.team
Models
class Team(models.Model):
name = models.CharField(max_length=10)
def __str__(self):
"""Return a string representation of the model."""
return self.name
class TeamsWeeklyMasterSchedule(models.Model):
"""Hours Available For That Day"""
team = models.ForeignKey(Team, on_delete=models.CASCADE)
class EmloyeeProfile(models.Model):
owner = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, )
team = models.ForeignKey(Team, on_delete=models.CASCADE,)
View
#login_required
def employee(request):
"""The home page"""
profile = EmployeeProfile.objects.filter(owner=request.user)
# I Cannot make this get() work!
teams_weekly_master_schedule = TeamsWeeklyMasterSchedule.objects.get()
context = {
'profile': profile,
'teams_weekly_master_schedule': teams_weekly_master_schedule,
}
return render(request, 'portal/employee.html', context)
What I've Tried
teams_weekly_master_schedule = TeamsWeeklyMasterSchedule.objects.get(team=profile.team)
teams_weekly_master_schedule = TeamsWeeklyMasterSchedule.objects.get(team=request.user.team)
I was able to replicate the problem and I solved it with this:
def employee(request):
"""The home page"""
profile = EmployeeProfile.objects.get(owner=request.user)
teams_weekly_master_schedule = TeamsWeeklyMasterSchedule.objects.get(team = profile.team)
context = {
'profile': profile,
'teams_weekly_master_schedule': teams_weekly_master_schedule,
}
return render(request, 'test.html', context)
The first problem was that on line 3, you used filter to retrieve one EmployeeProfile object. Using filter returns a queryset, which can't be accessed as an object.
When you use GET, you can use profile.team which returns the team you want.

django subcategory slug filter

Getting my head around Django and followed the tango with Django book, but this last issue gets me after adding subcategories, which are not included in that tutorial.
I have the following:
models.py
class Category(models.Model):
"""Category"""
name = models.CharField(max_length=50)
slug = models.SlugField()
def save(self, *args, **kwargs):
#self.slug = slugify(self.name)
self.slug = slugify(self.name)
super(Category, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
class SubCategory(models.Model):
"""Sub Category"""
category = models.ForeignKey(Category)
name = models.CharField(max_length=50)
slug = models.SlugField()
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(SubCategory, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
and
urls.py
(r'^links/$', 'rango.views.links'),
(r'^links/(?P<category_name_slug>[\w\-]+)/$', 'rango.views.category'),
(r'^links/(?P<category_name_slug>[\w\-]+)/(?P<subcategory_name_slug>[\w\-]+)/$', 'rango.views.subcategory'),
and
views.py
#require_GET
def links(request):
"""Linkdirectory Page"""
category_list = Category.objects.order_by('name')
context_dict = {'categories': category_list}
return render(request, 'links.html', context_dict)
#require_GET
def category(request, category_name_slug):
"""Category Page"""
category = Category.objects.get(slug=category_name_slug)
subcategory_list = SubCategory.objects.filter(category=category)
context_dict = {'subcategories': subcategory_list}
return render(request, 'category.html', context_dict)
#require_GET
def subcategory(request, subcategory_name_slug, category_name_slug):
"""SubCategory Page"""
context_dict = {}
try:
subcategory = SubCategory.objects.get(slug=subcategory_name_slug)
context_dict['subcategory_name'] = subcategory.name
websites = Website.objects.filter(sub_categories=subcategory)
context_dict['websites'] = websites
context_dict['subcategory'] = subcategory
except SubCategory.DoesNotExist:
return render(request, 'subcategory.html', context_dict)
This all works nicely up to the point I add subcategories with the same name, e.g. the subcategory "other" for multiple categories.
I understand why, when I reach "def subcategory" my slug will return multiple subcategories so I need to limit these to the related category in some way, like a
"SELECT
subcategory = SubCategory.objects.get(slug=subcategory_name_slug)
WHERE
subcategory = SubCategory.objects.filter(category=subcategory)
CLAUSE"
or something ;)
Not sure what's the best route to take on this and how to filter these
Given that you may have two different SubCategory objects with the same name for two different Category objects, as you suggested you can add the Category as an additional filter.
Filter by both SubCategory.slug and Category.slug
To achieve this I see you have a view that takes slugs for both SubCategory and Category, that you defined like this subcategory(request, subcategory_name_slug, category_name_slug). Those are sufficient to filter:
subcategory = SubCategory.objects.get(
slug=subcategory_name_slug,
category__slug=category_name_slug
)
^
|__ # This "double" underscore category__slug is a way to filter
# a related object (SubCategory.category)
# So effectively it's like filtering for SubCategory objects where
# SubCategory.category.slug is category_name_slug
You see above I've used SubCateogry.objects.get(...) to get a single object instead of `SubCategory.objects.filter(...) which can return many objects.
Enforcing unicity of SubCategory.name per Category
To do this safely with get(), there needs to be a guarantee that for any given Category, there will no more than one Subcategory with the same name
You can enforce this condition with unique_together
class SubCategory(models.Model):
class Meta:
unique_together = (
('category', 'name'), # since slug is based on name,
# we are sure slug will be unique too
)

Categories