Get django object id based on model attribute
Like this: place = Places.objects.get(name=”kansas”) print(place.id)
Like this: place = Places.objects.get(name=”kansas”) print(place.id)
Remove user field from rendered form (using exclude or fields, https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#selecting-the-fields-to-use ) class CandidateForm(forms.ModelForm): class Meta: model = Candidate exclude = [“user”] Find user profile and set user field in the create view. class CandidateCreateView(CreateView): … def form_valid(self, form): candidate = form.save(commit=False) candidate.user = UserProfile.objects.get(user=self.request.user) # use your own profile here candidate.save() return HttpResponseRedirect(self.get_success_url())
From your template, add the following: {{ form.errors }} {{ form.non_field_errors }} Do you see any errors from the above?
When using RequestFactory, you are testing view with exactly known inputs. That allows isolating tests from the impact of the additional processing performed by various installed middleware components and thus more precisely testing. You can setup request with any additional data that view function expect, ie: request.user = AnonymousUser() request.session = {} My personal recommendation … Read more
Using reverse in your method works because reverse is called when the view is run. def my_view(request): url = reverse(‘blog:list-post’) … If you overrride get_success_url, then you can still use reverse, because get_success_url calls reverse when the view is run. class BlogCreateView(generic.CreateView): … def get_success_url(self): return reverse(‘blog:list-post’) However, you can’t use reverse with success_url, because … Read more
You can delete the key from the session like any other dictionary. del request.session[‘your key’] You may need to mark the session as modified for it to save, depending on some of your settings. request.session.modified = True
Add an order_by clause to ensure it orders by number. next_issue = Issue.objects.filter(title=title, number__gt=issue.number).order_by(‘number’).first()
Typically, they are used to put each application’s URLs into their own namespace. This prevents the reverse() Django function and the {% url %} template function from returning the wrong URL because the URL-pattern name happened to match in another app. What I have in my project-level urls.py file is the following: from django.conf.urls.defaults import … Read more
Assuming from the error that eventList is a many-to-many field, you need to use .all() to get the related objects. A many-to-many field is a manager, so you can use it to construct querysets that return the actual objects. user = request.user.get_profile() eL = user.eventList.all()
I’ve been looking for the same, and turns out there are some good options. These are the best libraries I could find on GitHub: JSON Editor (plain JS) (demo) Angular Schema Form (AngularJS) (demo) React JSONSchema Form (React) (demo)