Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #models.py
- from django.db import models
- class Article(models.Model):
- title = models.CharField(max_length=100)
- content = models.TextField()
- @classmethod
- def create_article(cls, title, content):
- article = cls(title=title, content=content)
- article.save()
- return article
- def delete_article(self):
- self.delete()
- #how to use it in views.py
- # Create a new article
- new_article = Article.create_article(title='My First Article', content='This is the content of my article.')
- # Delete an article
- article_to_delete = Article.objects.get(pk=1) # Assuming the article you want to delete has a primary key of 1
- article_to_delete.delete_article()
Advertisement
Add Comment
Please, Sign In to add comment