django-forms

How to determine if a field has changed in a Django modelform

限于喜欢 提交于 2019-12-19 06:26:07
问题 I was surprised that this was difficult to do. However I came up with this, which seems to work at least for my simple case. Can anyone recommend a better approach? def field_changed(self, fieldname): """Tests if the value of the field changed from the original data""" orig_value = self.fields[fieldname].initial or getattr(self.instance, field, None) orig_value = getattr(orig_value, 'pk', orig_value) if type(orig_value) is bool: # because None and False can be interchangeable return bool(self

Dynamically Delete inline formsets in Django

青春壹個敷衍的年華 提交于 2019-12-19 06:14:22
问题 Is it possible to have Django automatically delete formsets that are not present in the request? So for example if I had three inline formsets represented in HTML when I loaded my edit page and I use javascript to remove two of those when the request is processes Django sees that those two forms are no longer their and deletes them. 回答1: Yes, it's possible using a few different methods. First is to copy the way it's done in the Django admin app, which is to have a checkbox with a label

Django Formset management-form validation error

我与影子孤独终老i 提交于 2019-12-19 05:44:08
问题 I have a form and a formset on my template. The problem is that the formset is throwing validation error claiming that the management form is "missing or has been tampered with". Here is my view @login_required def home(request): user = UserProfile.objects.get(pk=request.session['_auth_user_id']) blogz = list(blog.objects.filter(deleted='0')) delblog = modelformset_factory(blog, exclude=('poster','date' ,'title','content')) if request.user.is_staff== True: staff = 1 else: staff = 0 staffis =

Allowing Edit to editable=False Fields in Django Admin

◇◆丶佛笑我妖孽 提交于 2019-12-19 04:09:36
问题 DRF will use the editable=False on a field to default the Serializer to read-only. This is a very helpful / safe default that I take advantage of (ie I won't forget to set the Serializer to read-only). That being said once I have set editable=False is there any way to then force the Django admin to allow editing one of those fields? Presumably the admin is a super user and I do want him to be able to change the fields value but fore safety I want the default Serializer logic to be read only.

combine modelformset and inlineformset in django views

吃可爱长大的小学妹 提交于 2019-12-19 04:09:28
问题 i have two models class A(models.Model): name = models.CharField(50) type = models.CharField(50) class B(models.Model): field1 = ForeignKeyField(A) value = IntegerField() I'd like to display a number of forms via a ModelFormSet (from model A) where each one of the forms displays in turn InlineFormSets (from model B) for all objects connected to the object. How can i combine both modelformsets (model A) and inline formsets (mdoel B) on save method in django views? 回答1: I'm not sure this is a

Django self.cleaned_data Keyerror

折月煮酒 提交于 2019-12-18 17:32:25
问题 I'm writing a Django website and i'm writing my own validation for a form : class CreateJobOpportunityForm(forms.Form): subject = forms.CharField(max_length=30) start_date = forms.DateField(widget=SelectDateWidget) end_date = forms.DateField(widget=SelectDateWidget) def clean_start_date(self): start_date = self.cleaned_data['start_date'] end_date = self.cleaned_data['end_date'] if start_date > end_date : raise forms.ValidationError("Start date should be before end date.") return start_date

Django self.cleaned_data Keyerror

这一生的挚爱 提交于 2019-12-18 17:32:02
问题 I'm writing a Django website and i'm writing my own validation for a form : class CreateJobOpportunityForm(forms.Form): subject = forms.CharField(max_length=30) start_date = forms.DateField(widget=SelectDateWidget) end_date = forms.DateField(widget=SelectDateWidget) def clean_start_date(self): start_date = self.cleaned_data['start_date'] end_date = self.cleaned_data['end_date'] if start_date > end_date : raise forms.ValidationError("Start date should be before end date.") return start_date

Extending the user profile in Django. Admin creation of users

馋奶兔 提交于 2019-12-18 17:27:21
问题 Good evening, I am presently creating a site with Django and I extended the user with a user profile. I have a small problem though. Here is my situation: I extended the user profile in order to add custom fields. I added the model to the User Admin Model, so when I am adding a user, I can fill in directly the fields to create the profile. Now, if I don't add ANYTHING in these new custom user fields, in the user add page, Django Admin won't throw me an error saying these fields are null (and

Extending UserCreationForm to include email, first name, and last name

穿精又带淫゛_ 提交于 2019-12-18 17:22:10
问题 I've been stuck on this for a while now and can't seem to figure out what's going on. I'm just starting to learn Django and I got my login set up and now want to implement a registration page. I used the UserCreationForm form at first and that worked fine, but I want to add fields for Email, First name, and Last name. I figured I could just subclass UserCreationForm and add the fields but that doesn't seem to work. Also I tried overriding the save method, but it still doesn't work. My custom

Django CSRF verification failed. Request aborted.- CSRF cookie not set

若如初见. 提交于 2019-12-18 16:58:28
问题 I know this question has been asked before. I have tried almost all the options given by people but I cannot seem to solve it. I am a complete newbie so please let me know where I am going wrong. I am trying to write a simple raw form. I have not implemented any authentication or session mechanism until now (but from what I have read that does not matter to this problem. Correct me if I am wrong). When I try to submit my form, I get this error: Forbidden (403) CSRF verification failed.