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

EFSArCBy | Jul 20, 26 08:40

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

-1 OR 2+723-723-1=0+0+0+1 --

EFSArCBy | Jul 20, 26 08:41

-1 OR 2+503-503-1=0+0+0+1

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

-1' OR 2+618-618-1=0+0+0+1 or 'ygi7FfXP'='

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

555U3IrjwFJ' OR 233=(SELECT 233 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:41

555NdIb2z4Y') OR 485=(SELECT 485 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:41

555ttBc66yy')) OR 433=(SELECT 433 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

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

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555'"

EFSArCBy | Jul 20, 26 08:41

@@EMrQ1

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

-1 OR 2+469-469-1=0+0+0+1 -- | Jul 20, 26 08:41

555

-1 OR 2+242-242-1=0+0+0+1 | Jul 20, 26 08:41

555

-1' OR 2+76-76-1=0+0+0+1 -- | Jul 20, 26 08:41

555

-1' OR 2+461-461-1=0+0+0+1 or 'BrRTFRxU'=' | Jul 20, 26 08:41

555

-1" OR 2+533-533-1=0+0+0+1 -- | Jul 20, 26 08:41

555

if(now()=sysdate(),sleep(15),0) | Jul 20, 26 08:41

555

EFSArCBy0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z | Jul 20, 26 08:41

555

EFSArCBy0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z | Jul 20, 26 08:41

555

EFSArCBy-1 waitfor delay '0:0:15' -- | Jul 20, 26 08:41

555

EFSArCByelxIoonD'; waitfor delay '0:0:15' -- | Jul 20, 26 08:41

555

EFSArCByj5Lf8eMf' OR 335=(SELECT 335 FROM PG_SLEEP(15))-- | Jul 20, 26 08:41

555

EFSArCBy2tM6mWXB') OR 810=(SELECT 810 FROM PG_SLEEP(15))-- | Jul 20, 26 08:41

555

EFSArCByAbt32f1Q')) OR 256=(SELECT 256 FROM PG_SLEEP(15))-- | Jul 20, 26 08:41

555

EFSArCBy'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||' | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy'" | Jul 20, 26 08:41

555

@@4X7IH | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:41

555

EFSArCBy | Jul 20, 26 08:42

555

EFSArCBy | Jul 23, 26 11:29

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

-1 OR 2+717-717-1=0+0+0+1 --

EFSArCBy | Jul 23, 26 11:30

-1 OR 2+45-45-1=0+0+0+1

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

-1' OR 2+649-649-1=0+0+0+1 or 'wjg1V0T4'='

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

555XN55gvmH' OR 197=(SELECT 197 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:30

555fWo6cEgT') OR 142=(SELECT 142 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:30

5555B9DJnbF')) OR 41=(SELECT 41 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

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

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555'"

EFSArCBy | Jul 23, 26 11:30

@@EvmB2

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:30

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

-1 OR 2+22-22-1=0+0+0+1 -- | Jul 23, 26 11:31

555

-1 OR 2+106-106-1=0+0+0+1 | Jul 23, 26 11:31

555

-1' OR 2+52-52-1=0+0+0+1 -- | Jul 23, 26 11:31

555

-1' OR 2+635-635-1=0+0+0+1 or 'LWL0yYVL'=' | Jul 23, 26 11:31

555

-1" OR 2+643-643-1=0+0+0+1 -- | Jul 23, 26 11:31

555

if(now()=sysdate(),sleep(15),0) | Jul 23, 26 11:31

555

EFSArCBy0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z | Jul 23, 26 11:31

555

EFSArCBy0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z | Jul 23, 26 11:31

555

EFSArCBy-1 waitfor delay '0:0:15' -- | Jul 23, 26 11:31

555

EFSArCByu1AklzSN'; waitfor delay '0:0:15' -- | Jul 23, 26 11:31

555

EFSArCByNiEqmI0A' OR 666=(SELECT 666 FROM PG_SLEEP(15))-- | Jul 23, 26 11:31

555

EFSArCByOQK1zifI') OR 351=(SELECT 351 FROM PG_SLEEP(15))-- | Jul 23, 26 11:31

555

EFSArCBypiVkCYoD')) OR 538=(SELECT 538 FROM PG_SLEEP(15))-- | Jul 23, 26 11:31

555

EFSArCBy'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||' | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy'" | Jul 23, 26 11:31

555

@@7zq98 | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

555

EFSArCBy | Jul 23, 26 11:31

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.