django-forms

Django ForeignKey field required despite blank=True and null=True

匆匆过客 提交于 2021-01-04 05:31:00
问题 I am using Django REST Framework and I have a MyNodel with a related MyOtherModel in a many-to-one relationship: models.ForeignKey(MyModel, related_name="my_other_models", blank=True, null=True) Although blank=True, null=True , when I try to post a MyModel JSON without a my_other_models field I get a "this field is required" error. 回答1: In your serializer, you need to add required=False . field = MyModelSerializer(required=False) 来源: https://stackoverflow.com/questions/40237969/django

django csrf_token in search result url

喜你入骨 提交于 2021-01-02 05:50:53
问题 Have csrf in search result url. Don't know why is there and how to remove it. Search works nice. Here is URL /search/?csrfmiddlewaretoken=675d1340034e094866d15a921407e3fc&q=testing here is view: def search(request): query = request.GET.get('q', '') rezult = [] if query: qset1 = ( Q(title__icontains=query) ) result = Product.objects.filter(qset1).distinct() if result.count() == 1: return HttpResponseRedirect(result[0].get_absolute_url()) return render_to_response('search/search.html',{'query':

django csrf_token in search result url

允我心安 提交于 2021-01-02 05:50:37
问题 Have csrf in search result url. Don't know why is there and how to remove it. Search works nice. Here is URL /search/?csrfmiddlewaretoken=675d1340034e094866d15a921407e3fc&q=testing here is view: def search(request): query = request.GET.get('q', '') rezult = [] if query: qset1 = ( Q(title__icontains=query) ) result = Product.objects.filter(qset1).distinct() if result.count() == 1: return HttpResponseRedirect(result[0].get_absolute_url()) return render_to_response('search/search.html',{'query':

Save formset in an UpdateView

泪湿孤枕 提交于 2021-01-01 06:45:06
问题 I have the models Entry and Meaning below with many-to-many relation. I need to create an update form/view to edit Entry and Meaning objects at the same time. I have also to be able to add more Meaning objects to an Entry object while editing it. I tried to use UpdateView and modelformset_factory as below. I can see the forms, but my view doesn't save the Meaning changes. I have two questions: How do I save the Meaning changes? How do I add or delete a Meaning object in this form/view? Models

Django - form_valid() vs save()

眉间皱痕 提交于 2020-12-30 07:40:10
问题 In django forms, for saving other data I usually use form_valid() but as I can also use save() method of formclass. Today I overrided save() instead of form_valid() and I got problem with my manytomanyfield. When using , the values of manytomanyfield are not saved but when I use form_valid() they start saving. Can anybody tell me the reason and what are the differences between both, which is the most convenient method and in what situation? Here is my overriding of save() method: class

Django ModelForm inheritance and Meta inheritance

ぐ巨炮叔叔 提交于 2020-12-30 07:38:48
问题 I have this ModelForm: class Event(forms.ModelForm): def __init__(self, *args, **kwargs): super(Event, self).__init__(*args, **kwargs) ##Here make some changes such as: self.helper = FormHelper() self.helper.form_method = 'POST' ##Many settings here which **i don't want to rewrite in 10 child classes** class Meta: model = Event exclude = something... widgets = some settings here also. And this child ModelForm: class UpgradedEvent(Event): def __init__(self, *args, **kwargs): super

Django ModelForm inheritance and Meta inheritance

ぃ、小莉子 提交于 2020-12-30 07:38:08
问题 I have this ModelForm: class Event(forms.ModelForm): def __init__(self, *args, **kwargs): super(Event, self).__init__(*args, **kwargs) ##Here make some changes such as: self.helper = FormHelper() self.helper.form_method = 'POST' ##Many settings here which **i don't want to rewrite in 10 child classes** class Meta: model = Event exclude = something... widgets = some settings here also. And this child ModelForm: class UpgradedEvent(Event): def __init__(self, *args, **kwargs): super

Select a valid choice. That choice is not one of the available choices

只愿长相守 提交于 2020-12-30 03:42:05
问题 In my app, i have a dropdown (department) that's depending on the previously selected value from a dropdown(faculty field). I am using ajax to get the new value which works fine. However, when i try saving the form, i get Select a valid choice. That choice is not one of the available choices. Here is my model from django.db import models from system.models import FacultyData, DepartmentData, SessionData, SemesterData, SettingsData # Create your models here. class SchoolFees(models.Model): fid

Select a valid choice. That choice is not one of the available choices

戏子无情 提交于 2020-12-30 03:39:45
问题 In my app, i have a dropdown (department) that's depending on the previously selected value from a dropdown(faculty field). I am using ajax to get the new value which works fine. However, when i try saving the form, i get Select a valid choice. That choice is not one of the available choices. Here is my model from django.db import models from system.models import FacultyData, DepartmentData, SessionData, SemesterData, SettingsData # Create your models here. class SchoolFees(models.Model): fid

Need guidance with FilteredSelectMultiple widget

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-30 02:23:46
问题 I am sorry if it question might turn to be little broad, but since I am just learning django (and I am just hobbyist developer) I need some guidance which, I hope, will help someone like me in the future since I could not find any clear and easily comprehensible guide on using this widget. With your answers and help I will try to make this question thread at least guide-ish. Material I found somewhat helpful for this topic: Django multi-select widget? Django: Replacement for the default