modelform

Django ModelForm instance with custom queryset for a specific field

南笙酒味 提交于 2019-12-29 03:35:05
问题 I have a model not unlike the following: class Bike(models.Model): made_at = models.ForeignKey(Factory) added_on = models.DateField(auto_add_now=True) All users may work at a number of factories and therefore their user profiles all have a ManyToManyField to Factory . Now I want to construct a ModelForm for Bike but I want the made_at list to consist of only factories at which the current user works. The idea is that users should be able to add bikes that they've assembled and enter which of

NOT NULL constraint failed: accounts_myuser.password

♀尐吖头ヾ 提交于 2019-12-24 17:44:24
问题 I am doing a web application in django where users can create accounts. I'm storing the users' passwords in plaintext as authentication in my system does not totally depend on password but also on the otp. The problem I'm facing all of a sudden(it worked fine earlier) at the POST request of registration, is "NOT NULL constraint failed: accounts_myuser.password". I tried deleting database and migrations and re-migrated but it didn't help. I'm giving the ModelForm and the Model(custom one)

Django UnicodeEncodeError when displaying formset: ascii codec can't encode characters

时间秒杀一切 提交于 2019-12-24 16:07:02
问题 I am trying to simply display Model formset in django template. I get the following error Here is what I am trying to display: the actual formset within a form In the view.py, here is the related code snippet: # # create Address Model Form Set # AddressFormSet = modelformset_factory( Address, form=businessForms.AddressModelForm ) if request.method == 'GET': businessModelForm = businessForms.BusinessModelForm( instance = business ) addressModelFormSet = AddressFormSet( queryset=Address.objects

Saving data from ModelForm

▼魔方 西西 提交于 2019-12-24 03:37:27
问题 I am new to Django and I'm trying to save data using ModelForm. Template 'Vlozit' has a ModelForm and when submitted, I want the data saved in the DB and the redirect to base.html that actually loads the data from the DB and lists the output. The problem is that all works fine but the data is not saved. Please help me find what I am missing. Thank you. Here's the model: class Customer(models.Model): Name = models.CharField(max_length=30) Description = models.CharField(max_length=150) Creation

Save custom field in Django ModelForm

半世苍凉 提交于 2019-12-24 01:38:14
问题 I am having trouble saving a custom field in a ModelForm. The field in question is a ModelChoiceField. I have added a save() method as shown in this question. However, when I use it I get an error: ImproperlyConfigured No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model. When I remove my custom save() method it works ok but doesn't save the custom field. What am I missing? class NewStoryForm(forms.ModelForm): class Meta: model = Story fields = ['title'

django ModelForm save() method issue

徘徊边缘 提交于 2019-12-23 19:28:06
问题 I have a model form: class SnippetForm(ModelForm): class Meta: model = Snippet exclude = ['author', 'slug'] and I want to be able to edit a particular instance by using this: def edit_snippet(request, snippet_id): #look up for that snippet snippet = get_object_or_404(Snippet, pk=snippet_id) if request.user.id != snippet.author.id: return HttpResponseForbidden() if request.method == 'POST': form = SnippetForm(data=request.POST, instance=snippet) if form.is_valid(): form.save() return

Why doesn't Django enforce my unique_together constraint as a form.ValidationError instead of throwing an exception?

元气小坏坏 提交于 2019-12-23 07:58:09
问题 Edit: While this post is a duplicate of Django's ModelForm unique_together validation, the accepted answer here of removing the 'exclude' from the ModelForm is a much cleaner solution than the accepted answer in the other question. This is a follow-up to this question. If I don't explicitly check the unique_together constraint in the clean_title() function, django throws an exception: IntegrityError at /journal/journal/4 duplicate key value violates unique constraint "journal_journal_owner_id

how can I change the modelform label and give it a custom name

我的未来我决定 提交于 2019-12-18 12:46:21
问题 I want to create a custom name for on of the labels in my modelform this is my forms.py class PostForm(forms.ModelForm): body = forms.CharField(widget=PagedownWidget) publish = forms.DateField( widget=forms.SelectDateWidget, initial=datetime.date.today, ) class Meta: model = Post fields = [ "title", "body", "author", "image", "image_url", "video_path", "video", "publish", "tags", "status" ] I want to change the instead of video I want it to say embed. I checked the documentation but didn't

Duplicate key value violates unique constraint while saving ModelForm

徘徊边缘 提交于 2019-12-14 04:16:53
问题 My views.py class UserProfileFormView(View): def post(self, request, *args, **kwargs): userform = UserForm(request.POST, prefix='users') userprofileform = UserProfileForm(request.POST, prefix='userprofiles') if userform.is_valid() and userprofileform.is_valid(): new_user = userform.save() new_userprofile = userprofileform.save(commit=False) new_userprofile.user = new_user new_userprofile.save() #### Error is here return HttpResponseRedirect(reverse('users:welcome')) else: userform = UserForm

How to do model level custom field validation in django?

末鹿安然 提交于 2019-12-14 03:58:07
问题 I have this model: class StudentIelts(Model): SCORE_CHOICES = [(i/2, i/2) for i in range(0, 19)] student = OneToOneField(Student, on_delete=CASCADE) has_ielts = BooleanField(default=False,) ielts_listening = FloatField(choices=SCORE_CHOICES, null=True, blank=True, ) ielts_reading = FloatField(choices=SCORE_CHOICES, null=True, blank=True, ) ielts_writing = FloatField(choices=SCORE_CHOICES, null=True, blank=True, ) ielts_speaking = FloatField(choices=SCORE_CHOICES, null=True, blank=True, ) and