Guest User

Untitled

a guest
Jun 11th, 2023
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | Source Code | 0 0
  1. #models.py
  2.  
  3. from django.db import models
  4.  
  5. class Article(models.Model):
  6.     title = models.CharField(max_length=100)
  7.     content = models.TextField()
  8.  
  9.     @classmethod
  10.     def create_article(cls, title, content):
  11.         article = cls(title=title, content=content)
  12.         article.save()
  13.         return article
  14.  
  15.     def delete_article(self):
  16.         self.delete()
  17.  
  18.  
  19. #how to use it in views.py
  20. # Create a new article
  21. new_article = Article.create_article(title='My First Article', content='This is the content of my article.')
  22.  
  23. # Delete an article
  24. article_to_delete = Article.objects.get(pk=1)  # Assuming the article you want to delete has a primary key of 1
  25. article_to_delete.delete_article()
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment