understanding try except python/django

I need your help with understanding try-except python/Django. so I have this function: def submit_dept_head_application(request, application_id): cv = request.FILES[‘cv’] letter = request.FILES[‘letter’] candidate_id = request.data[‘candidateId’] rank_id = request.data[‘requestedRankId’] application_state = { ‘candidate_id’: candidate_id, ‘rank_id’: rank_id, ‘cv_filename’: cv.name, ‘letter_filename’: letter.name, } creator = Profile.objects.get(user=request.user.id) department = creator.department applicant = Profile.objects.get(user=candidate_id) applicant_profile_id = applicant.id rank = Rank.objects.get(id=rank_id)… Read More understanding try except python/django

Filtering out a foreignkey model out of a foreignkey model

Tryna filter all productimg objects of MainProduct but getting ValueError Cannot use QuerySet for "MainProduct": Use a QuerySet for "Product". but can’t use slug or id to filter it out of Product cuz it’s homepage home:view [doesnt work] #can’t use slug here def home(request): mainproduct = MainProduct.objects.all() productimg = ProductImage.objects.filter( #wanna access all productimg objects… Read More Filtering out a foreignkey model out of a foreignkey model

Django / OnetoMany relation within the same class

Here is my models.py class Scenes(models.Model): name = models.SlugField(‘Scene name’, max_length=60,unique=True) record_date = models.DateTimeField(‘Scene date’) manager = models.ForeignKey( settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL) description = models.TextField(blank=True) previous = models.OneToOneField( ‘self’, blank=True, null=True, related_name=’next’, on_delete=models.SET_NULL ) I have started with one instance : Scene1 The problem is I want to have on to many choices and not… Read More Django / OnetoMany relation within the same class

How to Update Django Model Relationship only with Foreign Key Value

I’m looking for a way to update a relationship on a model in Django by only passing the foreign key value and not the model being referenced in the relationship. I understand that Django wants me to do this publisher = Publisher.objects.get(id=2) book = Book.objects.get(id=1) book.update(publisher=publisher) But I’m looking to just update the publisher using… Read More How to Update Django Model Relationship only with Foreign Key Value

NoReverseMatch at /allbook Reverse for 'random_book' with arguments '('',)' not found. 1 pattern(s) tried: ['info/(?P<pk>[0-9]+)\\Z']

NoReverseMatch at /allbook Reverse for ‘random_book’ with arguments ‘(”,)’ not found. 1 pattern(s) tried: [‘info/(?P[0-9]+)\Z’] views.py class MoreInfoView(View): def get(self, request, id): book_info = BookModel.objects.filter(id=id).first() stuff = get_object_or_404(BookModel, id=self.kwargs[‘id’]) total_likes = stuff.total_likes() return render(request, ‘bookapp/more_info.html’, context={ ‘id’: id, ‘book_info’: book_info, ‘book’: BookModel, ‘total_likes’: total_likes, }) def random_book(self): book_pks = list(BookModel.objects.values_list(‘id’, flat=True)) pk = random.choice(book_pks) book… Read More NoReverseMatch at /allbook Reverse for 'random_book' with arguments '('',)' not found. 1 pattern(s) tried: ['info/(?P<pk>[0-9]+)\\Z']

Can't aggregate a DecimalField in Django

I have a queryset where I want to aggregate the fields amount which itself is a DecimalField. The target field is also a DecimalField. I get this error: django.core.exceptions.ValidationError: ["“{‘amount__sum’: Decimal(‘3821.02000000000’)}” value must be a decimal number."] Why does it say it must be a decimal number even though it is a DecimalField? # models.py… Read More Can't aggregate a DecimalField in Django

Filtering Base Django User by multiple groups

In my Django project I’m using base Django User and Group models. My goal is to get User queryset containing all User object who are assigned to multiple groups at the same time. For example I two groups and three users: from django.contrib.auth.models import User, Group a = Group.objects.create(name=’a’) b = Group.objects.create(name=’b’) user_a = User.objects.create_user(‘a’,… Read More Filtering Base Django User by multiple groups

How can I find how much 1 star/2 star—-5star present in percentage?

I’ve created a form for being stored ratings and feedback in the database. Ratings and Feedback are being stored in the database perfectly. But the problem is, I can’t find out how many different types of rating stars are present in the database. How can I find out how many 1 star/2star/—5 stars are present… Read More How can I find how much 1 star/2 star—-5star present in percentage?