django-views

Django Get ID of the object in save()

亡梦爱人 提交于 2021-02-10 07:07:09
问题 I have some fixed number let say it is num = 1000 Now I have field which need to be sum of the object.id and num. I need this in save() something like below. def save(self, *args, **kwargs): num = 1000 self.special = self.id + num super(MyModel, self).save(*args, **kwargs) But self.id is known after save and field named special is required. How to get self.id? 回答1: First of all? Is the number you're trying to add a fixed number? If so, why do you have to store it in the db at all? You may

allow post requests in django REST framework

独自空忆成欢 提交于 2021-02-09 11:12:38
问题 I am creating a simple rest api using django REST framework. I have successfully got the response by sending GET request to the api but since I want to send POST request, the django rest framework doesn't allow POST request by default. As in image(below) only GET,HEAD, OPTIONS are allowed but not the POST request The GET and POST methods inside of views.py from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from profiles

allow post requests in django REST framework

谁都会走 提交于 2021-02-09 11:11:07
问题 I am creating a simple rest api using django REST framework. I have successfully got the response by sending GET request to the api but since I want to send POST request, the django rest framework doesn't allow POST request by default. As in image(below) only GET,HEAD, OPTIONS are allowed but not the POST request The GET and POST methods inside of views.py from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from profiles

django - Unsupported lookup for JSONField or join on the field not permitted

爱⌒轻易说出口 提交于 2021-02-09 11:10:16
问题 I am having a Json field in my models as - class Product(models.Model): ... detailed_stock = JSONField(load_kwargs={'object_pairs_hook': collections.OrderedDict},default=dict) I am having values in my database like - { "total":0, "5[1]":0 } I am trying to filter objects with total = 0, for that I tried - Product.objects.filter(detailed_stock__total = 0) but it throws error - Unsupported lookup 'total' for JSONField or join on the field not permitted. as per the documentation the following

django how to assert url pattern resolves to correct class based view function

送分小仙女□ 提交于 2021-02-08 12:40:30
问题 I have a class based view class HomePage(View): def get(self, request): return HttpResponse('<p>This is content.</p>') and url-pattern defined as below: urlpatterns = patterns('', url(r'^$', HomePage.as_view()), ) To this pattern resolves to current view function, I wrote a test like this: class HomePageTest(TestCase): def test_root_url_resolves_to_home_page_view(self): found = resolve('/') self.assertIsInstance(found.func, HomePage) By running this unittest I am getting following error: self

How to handle to GET request in in same FBV(function based View.)

我与影子孤独终老i 提交于 2021-02-08 10:40:19
问题 Hello Everyone I am a beginner in Django and I have created one webpage where the user is able to search/Filter results and for this, I have used the Django-filter concept. Now my requirement is user should be able to download that filtered data. To achieve this I have created one separate View class with a download name and trying to pass the same Filter Class Queryset but with no luck, it is giving me all model data (records). Please find the Post: how to use Django filtered class data to 2

Django: Accessing URL variables in Class Based Views and Forms

人盡茶涼 提交于 2021-02-08 07:55:45
问题 I'm trying to create an object in Django using the standard class based views and form libraries. Three fields in my form are dependent upon a domain variable captured from the URL pattern. My questions are: How do I access domain within CreateSubscription so that I can set Subscription.site to Site.objects.filter(domain=domain)[0] ? How do I limit the dropdown fields rendered from CreateSubscriptionForm so that plan displays only SubscriptionPlan.objects.filter(site=Site.objects.filter

Filter queryset with multiple checks, including value_is_in array in a Django view

[亡魂溺海] 提交于 2021-02-08 07:37:22
问题 I need to check if a user exists before saving a form. The fields are name, surname, and a role which can have one or many values. How can I do that? def save(self, commit=True): profile = super(ProfileForm, self).save(commit=False) first_name = self.cleaned_data['first_name'] surname = self.cleaned_data['surname'] role = self.cleaned_data['role'] if Profile.objects.filter(first_name=self.cleaned_data['first_name'], surname=self.cleaned_data['surname']).exists() raise forms.ValidationError(

Django: Redirect to Detail View after Creation

烈酒焚心 提交于 2021-02-08 06:34:24
问题 I'd like to redirect to a detail view after I successfully submitted a form and created the object. My view.py class ObjectCreateView(CreateView): model = Object form_class = ObjectCreateForm template_name = 'frontend/base/object_create.html' def get(self, request, *args, **kwargs): form = ForecastConfigurationCreateForm() form.fields['status'] = ModelChoiceField(queryset=ObjectStatus.get_object_status_list(self)) return render(request, self.template_name, {'form': form}) def post(self,

How to make django register users with emails instead of username

帅比萌擦擦* 提交于 2021-02-08 03:45:27
问题 I am trying to make a website that registers users with first name, last name, email, and password. I have tried writing this form and using it in the views from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm class RegisterationForm(UserCreationForm): email = forms.EmailField(required=True) class Meta: model = User fields = ( 'first_name', 'last_name', 'email', 'password1', 'password2', ) def save(self, commit=True): user