Re: ModelForm unique validation is not done right IMHO.

2018-09-24 Thread Protik
Yes, it is working now. Thank you very much. On Monday, September 24, 2018 at 1:43:00 PM UTC+5:30, Todor Velichkov wrote: > > Protik, just use `self.fields['user'].initial = user.id` instead of just > `user`. Just tested it and it work fine. > > On Monday, September 24, 2018 at 9:36:19 AM

Re: ModelForm unique validation is not done right IMHO.

2018-09-24 Thread Todor Velichkov
One more thing I forgot to add I would add a clean_book method on the form and use it to check if the user > already have a book with that name. > You actually can't to this, because this just a simplified example where we have two fields only, what if we have a 3rd one and two of the fields

Re: ModelForm unique validation is not done right IMHO.

2018-09-24 Thread Todor Velichkov
> > Then the form will raise a Validation error saying there is already a book > with that user and that name. Confusing the user. > What error message are you gonna put instead? Book with that name already exists? This is even more confusing, because its actually allowed to have more books

Re: ModelForm unique validation is not done right IMHO.

2018-09-24 Thread Todor Velichkov
Protik, just use `self.fields['user'].initial = user.id` instead of just `user`. Just tested it and it work fine. On Monday, September 24, 2018 at 9:36:19 AM UTC+3, Protik wrote: > > > Book.objects.filter(user=form.user, name=form.cleaned_data["name"]). > exists() > > This will only work for

Re: ModelForm unique validation is not done right IMHO.

2018-09-24 Thread Protik
Book.objects.filter(user=form.user, name=form.cleaned_data["name"]). exists () This will only work for creating records, not updating. But what's the issue with the current solution posted by Todor. If you look at the model form it looks like this: class BookForm(forms.ModelForm): class

Re: ModelForm unique validation is not done right IMHO.

2018-09-24 Thread ludovic coues
First, that's a discussion for the Django user list, not the Django developers one. I would add a clean_book method on the form and use it to check if the user already have a book with that name. For that specific problems, that's the cleanest solution in my opinion. Simply do a

Re: ModelForm unique validation is not done right IMHO.

2018-09-23 Thread Protik
I am using Django 1.11. Further, adding a new book with user field disabled results in the following error: [image: Selection_046.png] I have attached the code to reproduce the error. On Monday, September 24, 2018 at 1:59:31 AM UTC+5:30, Todor Velichkov wrote: > > First thought: What is your

Re: ModelForm unique validation is not done right IMHO.

2018-09-23 Thread Protik
I am using Django 1.11. Further, adding a new book with user field disabled results in the following error: [image: Selection_046.png] I have attached the code to reproduce the error. On Monday, September 24, 2018 at 1:59:31 AM UTC+5:30, Todor Velichkov wrote: > > First thought: What is your

Re: ModelForm unique validation is not done right IMHO.

2018-09-23 Thread Protik
I am using Django 1.11. Further, adding a new book without disabling the user field results in the following error: [image: Selection_046.png] I have attached the code to reproduce this error. On Monday, September 24, 2018 at 1:59:31 AM UTC+5:30, Todor Velichkov wrote: > > First thought: What

Re: ModelForm unique validation is not done right IMHO.

2018-09-23 Thread Todor Velichkov
First thought: What is your Django version? The `disabled` attribute was added in Django 1.9. However by looking at your code (w/o testing anything) after `form.is_valid()` you should only call `form.save()`, no need to do `commit=False` and manually assigning user, you have already done that

Re: ModelForm unique validation is not done right IMHO.

2018-09-23 Thread Protik
On Sunday, September 23, 2018 at 11:55:41 PM UTC+5:30, Protik wrote: > > Hi, Todor > > I have tested this solution and It looks like it works only when you don't > disable the field (i.e the last line in the BookForm's `__init__()` method. > My views looks like this: > > > > def

Re: ModelForm unique validation is not done right IMHO.

2018-09-23 Thread Protik
Hi, Todor I have tested this solution and It looks like it works only when you don't disable the field (i.e the last line in the BookForm's `__init__()` method. My views looks like this: def book_add(request): user = get_object_or_404(User, id=1) if request.method == 'POST':

Re: ModelForm unique validation is not done right IMHO.

2018-09-23 Thread Todor Velichkov
You can use the `disabled ` attribute on form fields with a combination of HiddenInput Using the Book example from the first comment it will look like this:

Re: ModelForm unique validation is not done right IMHO.

2018-09-23 Thread Protik
Hi Todor I am experiencing the same problem. Can you please post the possible solution? On Tuesday, October 10, 2017 at 8:26:32 AM UTC+5:30, Todor Velichkov wrote: > > It does? Can you give me a link to that please? >> > > Pard me, it does not explicitly say to set values programmatically, but

Re: ModelForm unique validation is not done right IMHO.

2017-10-09 Thread Todor Velichkov
> > It does? Can you give me a link to that please? > Pard me, it does not explicitly say to set values programmatically, but that this is the place to go when fields depend on each other, and since clean is a multi-step process which does not include only field validation, but settings

Re: ModelForm unique validation is not done right IMHO.

2017-10-09 Thread Florian Apolloner
Hi, On Monday, October 9, 2017 at 8:52:53 AM UTC+2, Todor Velichkov wrote: > > Settings values programmatically is a cumulative operation most of the > time, however when its not and things depend on each other (like your > example), then even the docs suggests than one can use the form.clean

Re: ModelForm unique validation is not done right IMHO.

2017-10-09 Thread Todor Velichkov
Settings values programmatically is a cumulative operation most of the time, however when its not and things depend on each other (like your example), then even the docs suggests than one can use the form.clean method. If there is some other dependency outside form.cleaned_data I would prefer

Re: ModelForm unique validation is not done right IMHO.

2017-10-08 Thread Florian Apolloner
On Saturday, October 7, 2017 at 5:07:31 PM UTC+2, Todor Velichkov wrote: > > I believe this could save a lot of headache to people. If there is no > guarantee that an instance cannot be saved after the form has been > validated, then do not give me an option to shoot myself into the foot. >

Re: ModelForm unique validation is not done right IMHO.

2017-10-07 Thread Todor Velichkov
Thank you for the replay Florian, I will think about what you said that Django shouldn't make any assumptions about fields outside the form, but my intuition tells me that there are more staff which we could be retrieved from the ModelForm's. Some of my initial thoughts: If the ModelForm

Re: ModelForm unique validation is not done right IMHO.

2017-10-07 Thread Florian Apolloner
I think the current behaviour is correct. Django shouldn't make any assumptions about fields not in the form since it doesn't know what will happen… In that sense I disagree with the statement: > From my point of view when I define a ModelForm I imagine that I have to only explain what the