django-views

Reformat Django REST Framework Serializer to get Output

只谈情不闲聊 提交于 2020-06-16 03:35:10
问题 I am trying to get data in a particular format but i'm not able to get the desired output. My Model: class Category(models.Model): name = models.CharField(max_length=40) class Expense(models.Model): category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="category_name") description = models.CharField(max_length=200) total_amount = models.IntegerField() class Expense_Details(models.Model): expense = models.ForeignKey(Expense, on_delete=models.CASCADE, related_name=

Django rest framework- calling another class-based view

て烟熏妆下的殇ゞ 提交于 2020-06-16 02:37:07
问题 I have pored over several similar posts (and Calling a class-based view of an app from another app in same project seemed promising, but does not work), but some are older and none quite work for me. Here's my setup (using Django==2.0.6, djangorestframework==3.8.2) I have a basic model (simplified here): from django.db import models class Resource(models.Model): name = models.CharField(max_length=100, null=False) I have a basic endpoint where I can list and create Resource instances: from

Django csrf token for Ajax

北城余情 提交于 2020-06-11 06:09:30
问题 I have given {% csrf_token %} inside the form. Do I have to give another {% csrf_token %} inside the AJAX $.ajax({ .......... )} ? <form method="post" data-validate-username-url="{% url 'validate_username' %}"> {% csrf_token %} {{ form.as_p }} <button type="submit">Sign up</button> </form> <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> <script> $("#id_username").change(function () { console.log($(this).val()); var form = $(this).closest("form"); $.ajax({ url: form.attr(

django filter queryset show variables on template [duplicate]

允我心安 提交于 2020-06-08 14:20:25
问题 This question already has an answer here : Django-Filter Package: How To Filter Objects To Create Stats And Not Lists (1 answer) Closed 4 days ago . Below is the views.py to my stats page. This page has a bunch of calculations based on my model objects. Works great. However when I apply django-filter to the data it does not change. Example filtering for only "short" trades or in the "last 7 days". I know that get_context_data is basically hardcoding the results and it will not be affected by

Detailview Object Relations

邮差的信 提交于 2020-05-30 07:29:26
问题 ;TLDR - After some solutions discovered, my final question is how can I, if at all, access models related to models related to the main detailview model? I'm trying to use a generic detailview to return an object and it's related object. In this example, a company like mcdonalds would have any sites (or locations). What I want the detailview to be able to show is the company detail, and the site detail related to the company. I'm stuck though. Dispite my efforts in not asking for help, I have

Detailview Object Relations

懵懂的女人 提交于 2020-05-30 07:29:14
问题 ;TLDR - After some solutions discovered, my final question is how can I, if at all, access models related to models related to the main detailview model? I'm trying to use a generic detailview to return an object and it's related object. In this example, a company like mcdonalds would have any sites (or locations). What I want the detailview to be able to show is the company detail, and the site detail related to the company. I'm stuck though. Dispite my efforts in not asking for help, I have

Add a friend system on django

岁酱吖の 提交于 2020-05-26 09:09:25
问题 I have been trying to add friend system in which the user can add and remove friends (other users), after finishing with the code, I found an error that when the logedin user tries to add a friend from other user's profile, the add friend button redirects to the logedin user profile making it imposible to add a new friend, it can just add himself as a friend. I personally think the error is on the views.py profile view. views.py (profile shows user's profile and change_friend is the one that

Django - How to make a form for a model's foreign keys?

≯℡__Kan透↙ 提交于 2020-05-25 04:31:50
问题 Here's what I'm trying to do. I'm wondering if someone can suggest a good approach: models.py: class Color(models.Model): name = models.CharField(... class Speed(models.Model): name = models.CharField(... class Dog(models.Model): name = models.CharField(... color = models.ForeignKey(Color... speed = models.ForeignKey(Speed... class DogRequest(models.Model): dog = models.ForeignKey(Dog... request_time = models.DateTimeField() Now I want to have a page where a user can enter or edit a

Django check for any exists for a query

邮差的信 提交于 2020-05-24 11:19:34
问题 In django how to check whether any entry exists for a query sc=scorm.objects.filter(Header__id=qp.id) This was how it was done in php if(mysql_num_rows($resultn)) { // True condition } else { // False condition } 回答1: Use count(): sc=scorm.objects.filter(Header__id=qp.id) if sc.count() > 0: ... The advantage over e.g. len() is, that the QuerySet is not yet evaluated: count() performs a SELECT COUNT(*) behind the scenes, so you should always use count() rather than loading all of the record

Django check for any exists for a query

前提是你 提交于 2020-05-24 11:18:01
问题 In django how to check whether any entry exists for a query sc=scorm.objects.filter(Header__id=qp.id) This was how it was done in php if(mysql_num_rows($resultn)) { // True condition } else { // False condition } 回答1: Use count(): sc=scorm.objects.filter(Header__id=qp.id) if sc.count() > 0: ... The advantage over e.g. len() is, that the QuerySet is not yet evaluated: count() performs a SELECT COUNT(*) behind the scenes, so you should always use count() rather than loading all of the record