Violating unique constraint in the admin

2021-10-08 Thread Fab
Hi,

When I violate a unique constraint in the admin I get an error page.

I know I can check this myself and show an error message when the form is 
cleaned but I feel like this should be handled by default.

Are there any reasons why it shouldn't show an error message on the admin 
page?

Cheers,

Fabian

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/f013784c-16b6-4f63-a7cd-089c353e252an%40googlegroups.com.


Re: Violating unique constraint in the admin

2021-10-08 Thread Tim Graham
Hi, there have been some past discussions, e.g. 
https://groups.google.com/g/django-developers/c/Dju2vvbzECI/m/vYDnYekgBQAJ

On Friday, October 8, 2021 at 6:34:02 AM UTC-4 Fab wrote:

> Hi,
>
> When I violate a unique constraint in the admin I get an error page.
>
> I know I can check this myself and show an error message when the form is 
> cleaned but I feel like this should be handled by default.
>
> Are there any reasons why it shouldn't show an error message on the admin 
> page?
>
> Cheers,
>
> Fabian
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/f9de59af-cf84-4218-a373-21fdcc0a3241n%40googlegroups.com.


Re: Why can't range function be added in templates?

2021-10-08 Thread 'Adam Johnson' via Django developers (Contributions to Django itself)
Django’s general policy is that templates should be kept as simple as
possible. It's possible to create range() objects in the view, place them
in the context, and then iterate over them in templates.

Also, adding range() for a project is a few lines of code with simple_tag:
https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/#simple-tags.
So it doesn't seem like much to ask users to add it when required.

On Sat, 2 Oct 2021 at 12:24, Dhruva Shaw  wrote:

> I did read this comment
> https://code.djangoproject.com/ticket/13088#comment:1 and it says its
> more like adding programming to templates! :(
>
> Isn't whole template a programming (made up of programming blocks)?? I
> mean there is a for loop and all , also there are some cases where you need
> a numeric iterator for which the the no items for the loop is directly
> coming from database eg
> https://github.com/Tanzanite-lpu/tgl-2.0.0/blob/ddce5ef87a4b5248b8cb8cd5fa3df4adb3f00b31/main/templates/groups.html#L36
>
> Also there maybe a case where you want to run a loop without a list.
>
> So I believe a range function should be added. :)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/dababcf6-3693-40cd-8c5c-5873592ed7c5n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMyDDM0cZL-9%3Dc932iEBvf1zzEEUV58aGZpGcxSHDq7ue39C-w%40mail.gmail.com.


Change the way values FK fields on a custom User model are handled for the 'createsuperuser' command

2021-10-08 Thread Christophe Henry

Hello Django developpers,

Following the discussion I had with Mariusz Felisiak on PR #14913 
, 
I would like to suggest a small change in the way the createsuperuser 
command processes values for foreign key fields of custom User models.


This all started as I was trying to override the createsuperuser command 
for a Django project I work on. This project has a custom user model 
with a FK field named `organisation`. As I was working on that, I 
noticed that, if I passed an instance of the Organisation model for the 
field organisation, I got a TypeError because createsuperuser performs a 
field.clean on the value which tries to convert a model into an integer. 
But if I pass an integer, I get a ValueError, because later in the code, 
the command calls the the AbstractUser constructor which expects an 
instance of Organisation for the organisation field.


It felt like a bug, so I opened #33151 
 and proposed to fix this 
in a PR that simply wraps the input value in a model 
.


But, during the review, Mariusz Felisiak informs me that this behaviour 
is documented 
 
as it is advised to always write a custom manager with the create_user 
and create_superuser methods and any change here is a breaking change 
that should be discussed on the developer's mailing list. Which is why I 
would like to make my case here.


I would like to stress how easy it is to miss that part of the 
documentation and run into this unexpected behaviour. Everywhere in the 
framework, when I use the name of an FK field, an instance of the model 
is expected. This is true for model constructors and every method of the 
ORM. Which is why it was disturbing to me seeing createsuperuser break 
this convention and use the name of the FK field to pass the primary key.


This means, every time I will use a custom user model in combination to 
REQUIRED_FIELDS, I will have to write a custom manager with create_user 
and create_superuser methods that do this:


   class CustomUserManager(UserManager):
   def __normalize_fields(self, extra_fields: dict):
    for field_name in extra_fields.keys():
    field = self.model._meta.get_field(field_name)
    field_value = extra_fields[field_name]

    if field.many_to_one and not isinstance(
    field_value, field.remote_field.model
    ):
    field_value = (
    int(field_value)
    if not isinstance(field_value, int)
    else field_value
    )
    extra_fields[field_name] =
   field.remote_field.model(field_value)

    def create_user(self, username, email=None, password=None,
   **extra_fields):
    self.__normalize_fields(extra_fields)
    return super().create_user(username, email, password,
   **extra_fields)

    def create_superuser(self, username, email=None, password=None,
   **extra_fields):
    self.__normalize_fields(extra_fields)
    return super().create_superuser(username, email, password,
   **extra_fields)

Even though it's this only thing it does. IMHO, the createsuperuser 
command could be — and should be — doing this itself. BTW, it's what's 
being done in interactive mode 
 
to check the validity of the user input, even though the wrapping is 
discarded immediatly after and only the raw value is transmitted. I 
don't really see why the command performs a check in this situation but 
not always.


--
/Cordialement/Regards,
Christophe Henry/
—
Christophe Henry
https://christophe-henry.dev/
Dev. Python/JS/Android & devops
Timon-Ethics / Coopaname 


--
You received this message because you are subscribed to the Google Groups "Django 
developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/ecde841c-0a68-681a-be85-3e4242385367%40timon-ethics.fr.


Re: Violating unique constraint in the admin

2021-10-08 Thread 'Adam Johnson' via Django developers (Contributions to Django itself)
See also https://code.djangoproject.com/ticket/30581 which hoists
constraint validation to forms in general, not just the admin.

On Fri, 8 Oct 2021 at 13:43, Tim Graham  wrote:

> Hi, there have been some past discussions, e.g.
> https://groups.google.com/g/django-developers/c/Dju2vvbzECI/m/vYDnYekgBQAJ
>
> On Friday, October 8, 2021 at 6:34:02 AM UTC-4 Fab wrote:
>
>> Hi,
>>
>> When I violate a unique constraint in the admin I get an error page.
>>
>> I know I can check this myself and show an error message when the form is
>> cleaned but I feel like this should be handled by default.
>>
>> Are there any reasons why it shouldn't show an error message on the admin
>> page?
>>
>> Cheers,
>>
>> Fabian
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/f9de59af-cf84-4218-a373-21fdcc0a3241n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMyDDM1-9OjfFt8fqbaYr7Fd%2BftRPnT3wz--iWqrb-yuQdMZKg%40mail.gmail.com.


Re: Why can't range function be added in templates?

2021-10-08 Thread Dhruva Shaw
Ik range function can be added but I also mentioned that there are some 
cases range function can't be direct generated from the view like this  
https://github.com/Tanzanite-lpu/tgl-2.0.0/blob/ddce5ef87a4b5248b8cb8cd5fa3df4adb3f00b31/main/templates/groups.html#L36

Well I did add the range with simple tag but there many beginners Django 
out there who dosen't know about this. 

Also where is the problem, since templates are almost pythonic in nature 
(with for loops and if else) so where is the problem adding a range 
function.

Range also give way more possible idea :)

On Friday, October 8, 2021 at 7:30:28 PM UTC+5:30 Adam Johnson wrote:

> Django’s general policy is that templates should be kept as simple as 
> possible. It's possible to create range() objects in the view, place them 
> in the context, and then iterate over them in templates.
>
> Also, adding range() for a project is a few lines of code with simple_tag: 
> https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/#simple-tags.
>  
> So it doesn't seem like much to ask users to add it when required.
>
> On Sat, 2 Oct 2021 at 12:24, Dhruva Shaw  wrote:
>
>> I did read this comment 
>> https://code.djangoproject.com/ticket/13088#comment:1 and it says its 
>> more like adding programming to templates! :(
>>
>> Isn't whole template a programming (made up of programming blocks)?? I 
>> mean there is a for loop and all , also there are some cases where you need 
>> a numeric iterator for which the the no items for the loop is directly 
>> coming from database eg 
>> https://github.com/Tanzanite-lpu/tgl-2.0.0/blob/ddce5ef87a4b5248b8cb8cd5fa3df4adb3f00b31/main/templates/groups.html#L36
>>
>> Also there maybe a case where you want to run a loop without a list.
>>
>> So I believe a range function should be added. :)
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django developers (Contributions to Django itself)" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-develop...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-developers/dababcf6-3693-40cd-8c5c-5873592ed7c5n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/def05c1d-e794-4709-86f9-082382aa95f7n%40googlegroups.com.


Re: Why can't range function be added in templates?

2021-10-08 Thread Dhruva Shaw
Ik range function can be added but I also mentioned that there are some 
cases range function can't be direct generated from the view like this  
https://github.com/Tanzanite-lpu/tgl-2.0.0/blob/ddce5ef87a4b5248b8cb8cd5fa3df4adb3f00b31/main/templates/groups.html#L36

Well I did add the range with simple tag but there many beginners Django 
users out there who dosen't know about this. 

Also where is the problem, since templates are almost pythonic in 
nature (with - for loops and if else) so where is the problem adding a 
simple default range function to templates.
This will in a way make django templates more powerful.

Range also give way more possible idea :)

On Friday, October 8, 2021 at 7:30:28 PM UTC+5:30 Adam Johnson wrote:

> Django’s general policy is that templates should be kept as simple as 
> possible. It's possible to create range() objects in the view, place them 
> in the context, and then iterate over them in templates.
>
> Also, adding range() for a project is a few lines of code with simple_tag: 
> https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/#simple-tags.
>  
> So it doesn't seem like much to ask users to add it when required.
>
> On Sat, 2 Oct 2021 at 12:24, Dhruva Shaw  wrote:
>
>> I did read this comment 
>> https://code.djangoproject.com/ticket/13088#comment:1 and it says its 
>> more like adding programming to templates! :(
>>
>> Isn't whole template a programming (made up of programming blocks)?? I 
>> mean there is a for loop and all , also there are some cases where you need 
>> a numeric iterator for which the the no items for the loop is directly 
>> coming from database eg 
>> https://github.com/Tanzanite-lpu/tgl-2.0.0/blob/ddce5ef87a4b5248b8cb8cd5fa3df4adb3f00b31/main/templates/groups.html#L36
>>
>> Also there maybe a case where you want to run a loop without a list.
>>
>> So I believe a range function should be added. :)
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django developers (Contributions to Django itself)" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-develop...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-developers/dababcf6-3693-40cd-8c5c-5873592ed7c5n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/906e7aa2-b814-4ed3-8866-0c33e08bdd77n%40googlegroups.com.