Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def update_category(request):
- if request.method=='POST':
- old_name=request.POST.get('old_name').strip() # get old name
- new_name=request.POST.get('new_name').strip() # get new name
- categorie=Category.objects.get(title=old_name) # get category by title (pk)
- categorie.title=new_name # update the title
- categorie.save() # save to the database
- return HttpResponseRedirect('/admin/Category_Management)
- class Category(models.Model):
- class Meta:
- verbose_name='Catégorie'
- verbose_name_plural='Catégories'
- title=models.CharField(primary_key=True,max_length=50,null=False,blank=False,verbose_name='Titre')
- def __str__(self):
- return self.title.title()
- class Category(models.Model):
- class Meta:
- verbose_name='Catégorie'
- verbose_name_plural='Catégories'
- title=models.CharField(max_length=50,null=False,blank=False,verbose_name='Titre')
- def __str__(self):
- return self.title.title()
- category = Category.objects.get(title='test')
- category.id = None
- category.title = 'new test'
- category.save() # it will make another category, with title 'new test'
Add Comment
Please, Sign In to add comment