#35866: Django documentaion style guide on models is unclear what to do with any
other Python dunder methods that the model class might have
-------------------------------------+-------------------------------------
     Reporter:  Hristo Trendafilov   |                     Type:
                                     |  Uncategorized
       Status:  new                  |                Component:
                                     |  Documentation
      Version:                       |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 Following the discussion here:
 [https://github.com/astral-sh/ruff/issues/13892#issuecomment-2436995567]

 It turns out that the Django documentation regarding model structuring
 [https://docs.djangoproject.com/en/dev/internals/contributing/writing-code
 /coding-style/#model-style] is unclear.

 As proposed:


 {{{
 The order of model inner classes and standard methods should be as follows
 (noting that these are not all required):

     All database fields
     Custom manager attributes
     class Meta
     def __str__()
     def save()
     def get_absolute_url()
     Any custom methods
 }}}

 makes understanding that any other dunder, private methods and so on
 should come after the methods mentioned above,
 which violates Python class best practices of structuring code in classes,
 where dunder methods should be on top, then constants, staticmethods,
 private methods and so on...

 The following code is an example:

 What Python best practices suggests:

 {{{
 class Person(models.Model):
     name = models.CharField(xxxx)

     def __repr__(self):
         return "something"

     def save(*args, **kwargs):
         super().save(*args, `**kwargs)
 }}}

 What Django documentation suggests:

 {{{
 class Person(models.Model):
     name = models.CharField(xxxx)

     # Django methods
     def save(*args, **kwargs):
         super().save(*args, `**kwargs)

     # Any custom methods
     def __repr__(self):
         return "something"
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35866>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070192c3d1efa4-5747c0c4-0347-4736-ae21-e2d48c792319-000000%40eu-central-1.amazonses.com.

Reply via email to